Prepare parameters. $selection = array( 'log', 'fromCompany', 'fromGroup', 'fromDivision', 'fromProfile', 'toCompany', 'toGroup', 'toDivision', 'toProfile', ); $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.smsTo AS log_smsTo, log.smsBody AS log_smsBody, ' . '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.smsTo IS NOT NULL AND log.smsBody != \'\''; #-> Construct details. $authData = \Utility\Registry::getAuthData(); $clickVal = isset($this->_input['clickVal']) && is_numeric($this->_input['clickVal']) ? $this->_input['clickVal'] : 0.25; 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']) { error_log($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']; } #-> 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); $result = $query->getResult(); $data = array(); $counter = 1; while(($record = array_shift($result)) != null) { $tmpRecord = array(); $tmpRecord['fromGroup_name'] = $record['fromGroup_name']; $tmpRecord['fromDivision_name'] = $record['fromDivision_name']; $tmpRecord['fromCompany_name'] = $record['fromCompany_name']; $tmpRecord['fromProfile_firstName'] = $record['fromProfile_firstName']; $tmpRecord['fromProfile_familyName'] = $record['fromProfile_firstName']; $tmpRecord['log_created'] = $record['log_created']; $tmpRecord['toGroup_name'] = $record['toGroup_name']; $tmpRecord['toDivision_name'] = $record['toDivision_name']; $tmpRecord['toCompany_name'] = $record['toCompany_name']; $tmpRecord['toProfile_firstName'] = $record['toProfile_firstName']; $tmpRecord['toProfile_familyName'] = $record['toProfile_familyName']; $tmpRecord['log_smsTo'] = $record['log_smsTo']; $tmpRecord['log_smsBody'] = $record['log_smsBody']; $tmpRecord['charCount'] = strlen($record['log_smsBody']); $tmpRecord['clickCount'] = strlen($record['log_smsBody']) > 160 ? ceil(strlen($record['log_smsBody']) / (160 - 7)) : 1; $tmpRecord['clickValue'] = $clickVal; $tmpRecord['total'] = round($tmpRecord['charCount'] * $clickVal, 2); $data[] = $tmpRecord; $counter++; } $this->_data = $data; } }