Scalr\Model\Entity\Farm::findWithTeams PHP Method

findWithTeams() public static method

Searches farms by criteria and selecting and initiating their Teams
public static findWithTeams ( array $criteria = null, array $group = null, array $order = null, integer $limit = null, integer $offset = null, boolean $countRecords = null ) : ArrayCollection | Farm[]
$criteria array optional The search criteria.
$group array optional The group by looks like [property1, ...], by default groups by `id`
$order array optional The results order looks like [property1 => true|false, ... ]
$limit integer optional The records limit
$offset integer optional The offset
$countRecords boolean optional True to calculate total number of the records without limit
return Scalr\Model\Collections\ArrayCollection | Farm[] Returns collection of the entities.
    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;
    }