Prepare parameters. $selection = array( 'auction', 'stock', 'type', 'year', 'model', 'make', 'papers', 'sellerCompany', 'sellerRegion', 'sellerGroup', 'sellerDivision', 'sellerProfile', 'buyerCompany', 'buyerRegion', 'buyerGroup', 'buyerDivision', 'buyerProfile', 'COUNT(DISTINCT bid.id) as numBids' ); $join = array(); $leftJion = array(); $where = array(); $params = array(); $query = 'SELECT [SELECTION] ' . 'FROM \Auction\Entity\Auction auction ' . ' JOIN auction.stock stock ' . ' JOIN stock.company sellerCompany ' . ' JOIN sellerCompany.region sellerRegion ' . ' JOIN auction.bids bid ' . ' LEFT JOIN stock.type type ' . ' LEFT JOIN stock.vehicleYear year ' . ' LEFT JOIN type.model model ' . ' LEFT JOIN model.make make ' . ' LEFT JOIN stock.papers papers ' . ' LEFT JOIN auction.createdBy sellerProfile ' . ' LEFT JOIN auction.currentBid currentBid ' . ' LEFT JOIN currentBid.profile buyerProfile ' . ' LEFT JOIN currentBid.company buyerCompany ' . ' LEFT JOIN buyerCompany.region buyerRegion ' . ' LEFT JOIN sellerCompany.group sellerGroup ' . ' LEFT JOIN sellerCompany.groupDivision sellerDivision ' . ' LEFT JOIN buyerProfile.group buyerGroup ' . ' LEFT JOIN buyerProfile.groupDivision buyerDivision ' . '[WHERE] ' . 'GROUP BY auction.id ' . 'ORDER BY auction.created ASC'; $where[] = 'auction.jobState = \'Sold\''; #-> Construct details. $authData = \Utility\Registry::getAuthData(); if (isset($this->_input['dateFrom']) && !empty($this->_input['dateFrom'])) { $this->_queries['Date Range From'] = $this->_input['dateFrom']; $where[] = 'auction.endDate >= :dateFrom'; $params['dateFrom'] = new \DateTime($this->_input['dateFrom']); } if (isset($this->_input['dateTo']) && !empty($this->_input['dateTo'])) { $this->_queries['Date Range Until'] = $this->_input['dateTo']; $where[] = 'auction.endDate <= :dateTo'; $params['dateTo'] = new \DateTime($this->_input['dateTo'] . ' 23:59:59'); } if (isset($this->_input['group']) && !empty($this->_input['group']) && 'null' != $this->_input['group']) { $this->_queries['Group'] = $this->em ->find('\Company\Entity\Group', $this->_input['group']) ->name; $where[] = 'sellerGroup.id = :group'; $params['group'] = $this->_input['group']; } if (isset($this->_input['groupDivision']) && !empty($this->_input['groupDivision']) && 'null' != $this->_input['groupDivision']) { error_log($this->_input['groupDivision']); $this->_queries['Division'] = $this->em ->find('\Company\Entity\GroupDivision', $this->_input['groupDivision']) ->name; $where[] = 'sellerDivision.id = :division'; $params['division'] = $this->_input['groupDivision']; } if (isset($this->_input['company']) && !empty($this->_input['company']) && 'null' != $this->_input['company']) { $this->_queries['Dealership'] = $this->em ->find('\Company\Entity\Company', $this->_input['company']) ->name; $where[] = 'sellerCompany.id = :company'; $params['company'] = $this->_input['company']; } #-> Finalize query. $query = str_replace(array( '[SELECTION]', '[WHERE]' ), array( implode(', ', $selection) . ' ', !empty($where) ? 'WHERE ' . implode(' AND ', $where) . ' ' : ' ' ), $query); #-> Collect data. $query = $this->em->createQuery($query); !empty($params) && $query->setParameters($params); // \Utility\Debug::errorLog('$query->getSQL()', $query->getSQL()); // \Utility\Debug::errorLog('$query->getParameters()', $query->getParameters()); $this->_data = $query->getScalarResult(); $this->em->clear(); foreach ($this->_data as $id => $row) { $this->_data[$id]['bidPerc'] = 0 < $row['stock_tradePrice'] ? round((100 / $row['stock_tradePrice']) * $row['auction_currentBidPrice'], 1) . ' %' : '0.0 %'; $this->_data[$id]['papers_name'] = ''; if('Relist' != $row['auction_jobState']) { $this->_data[$id]['papers_name'] = $row['papers_name']; } } } }