Prepare parameters. $join = array(); $leftJion = array(); $where = array(); $params = array(); ini_set('memory_limit','512M'); $query = 'SELECT ' . 'fromCompany.name AS fromCompany_name, fromGroup.name AS fromGroup_name, fromDivision.name AS fromDivision_name, ' . 'fromProfile.firstName AS fromProfile_firstName, fromProfile.familyName AS fromProfile_familyName, ' . 'log.created AS log_created, log.emailTo AS log_emailTo, log.emailSubject AS log_emailSubject, ' . 'toCompany.name AS toCompany_name, toGroup.name AS toGroup_name, toDivision.name AS toDivision_name, ' . 'toProfile.firstName AS toProfile_firstName, toProfile.familyName AS toProfile_familyName ' . 'FROM \Utility\Entity\NotificationLog log ' . ' LEFT JOIN log.fromCompany fromCompany ' . ' LEFT JOIN fromCompany.group fromGroup ' . ' LEFT JOIN fromCompany.groupDivision fromDivision ' . ' LEFT JOIN log.fromProfile fromProfile ' . ' LEFT JOIN log.toCompany toCompany ' . ' LEFT JOIN toCompany.group toGroup ' . ' LEFT JOIN toCompany.groupDivision toDivision ' . ' LEFT JOIN log.toProfile toProfile ' . '[WHERE] ' . 'ORDER BY log_created ASC'; $where[] = 'log.emailTo IS NOT NULL'; #-> Construct details. 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[] = 'fromGroup.id = :group'; $params['group'] = $this->_input['group']; } if (isset($this->_input['groupDivision']) && !empty($this->_input['groupDivision']) && 'null' != $this->_input['groupDivision']) { $this->_queries['Division'] = $this->em ->find('\Company\Entity\GroupDivision', $this->_input['groupDivision']) ->name; $where[] = 'fromDivision.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[] = 'fromCompany.id = :company'; $params['company'] = $this->_input['company']; } if (isset($this->_input['profile']) && !empty($this->_input['profile']) && 'null' != $this->_input['profile']) { $profile = $this->_queries['User'] = $this->em ->find('\User\Entity\Profile', $this->_input['profile']); $this->_queries['User'] = $profile->firstName . ' ' . $profile->familyName; $where[] = 'fromProfile.id = :profile'; $params['profile'] = $this->_input['profile']; } #-> Finalize query. $query = str_replace(array( '[WHERE]' ), array( !empty($where) ? 'WHERE ' . implode(' AND ', $where) . ' ' : ' ' ), $query); #-> Collect data. $query = $this->em->createQuery($query); !empty($params) && $query->setParameters($params); $this->_data = $query->getResult(); } }