/** * Searches farms by criteria and selecting and initiating their Teams * * @param array $criteria optional The search criteria. * @param array $group optional The group by looks like [property1, ...], by default groups by `id` * @param array $order optional The results order looks like [property1 => true|false, ... ] * @param int $limit optional The records limit * @param int $offset optional The offset * @param bool $countRecords optional True to calculate total number of the records without limit * * @return ArrayCollection|Farm[] Returns collection of the entities. * * @throws \Scalr\Exception\ModelException */ public static function findWithTeams(array $criteria = null, array $group = null, array $order = null, $limit = null, $offset = null, $countRecords = null) { $farm = new Farm(); /* @var $farms Farm[] */ $farms = []; if (!isset($group)) { $group = ['id']; } $collection = $farm->result(AbstractEntity::RESULT_ENTITY_COLLECTION)->find($criteria, $group, $order, $limit, $offset, $countRecords); /* @var $farm Farm */ foreach ($collection as $farm) { $farms[$farm->id] = $farm; } $team = new Team(); $farmTeam = new FarmTeam(); $stmt = "\n SELECT {$team->fields()}, {$farmTeam->fields('ft', true)}\n FROM {$team->table()}\n JOIN {$farmTeam->table('ft')} ON {$farmTeam->columnTeamId('ft')} = {$team->columnId()}\n AND {$farmTeam->columnFarmId('ft')} IN ('" . implode("', '", array_keys($farms)) . "')\n "; foreach ($team->db()->Execute($stmt) as $row) { $team = new Team(); $team->load($row); $farmTeam->load($row, 'ft'); $farms[$farmTeam->farmId]->_teams[$team->id] = $team; } return $collection; }