Prepare parameters. $selection = array( 'log', 'tuhpi', 'stock', 'type', 'model', 'make', 'company', 'companyGroup', 'companyDivision', 'profile' ); $join = array(); $leftJion = array(); $where = array(); $params = array(); $query = 'SELECT [SELECTION] ' . 'FROM \Stock\Entity\TuhpiLog log ' . ' JOIN log.stock stock ' . ' JOIN log.company company ' . ' JOIN log.createdBy profile ' . ' LEFT JOIN log.tuhpi tuhpi ' . ' LEFT JOIN stock.type type ' . ' LEFT JOIN type.model model ' . ' LEFT JOIN model.make make ' . ' LEFT JOIN company.group companyGroup ' . ' LEFT JOIN company.groupDivision companyDivision ' . '[WHERE] ' . 'ORDER BY log.created ASC'; $where[] = ' tuhpi.hpi_type = :hpiType '; $params['hpiType'] = 'ACC'; #-> Construct details. $authData = \Utility\Registry::getAuthData(); if (isset($this->_input['dateFrom']) && !empty($this->_input['dateFrom'])) { $this->_queries['Date Range From'] = $this->_input['dateFrom']; $where[] = 'log.created >= :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[] = 'log.created <= :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[] = 'companyGroup.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[] = 'companyDivision.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[] = 'company.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); $this->_data = $query->getScalarResult(); $this->em->clear(); } }