public function _teams($from, $to, $action) { switch ($action) { case static::ACT_CONVERT_TO_OBJECT: /* @var $from Farm */ /* @var $team Team */ foreach ($from->getTeams() as $team) { $to->teams[] = ['id' => $team->id]; } break; case static::ACT_CONVERT_TO_ENTITY: /* @var $to Farm */ $newTeams = []; $newTeamIds = []; $user = $this->controller->getUser(); foreach ($from->teams as $teamFk) { $team = new Team(); $team->id = ApiController::getBareId($teamFk); $newTeams[] = $team; $newTeamIds[] = $team->id; } $currentTeamIds = []; foreach ($to->getTeams() as $team) { $currentTeamIds[] = $team->id; } sort($newTeamIds); sort($currentTeamIds); if ($newTeamIds != $currentTeamIds) { //filter out the Teams to which the User has no access $teams = empty($newTeams) ? [] : Team::find([['id' => ['$in' => $newTeamIds]], ['accountId' => $user->getAccountId()]]); if (count($teams) != count($newTeamIds)) { throw new ApiErrorException(404, ErrorMessage::ERR_OBJECT_NOT_FOUND, "Requested Team(s) either does not exist or Farm has no access to it."); } $to->setTeams($newTeams); } break; case static::ACT_GET_FILTER_CRITERIA: $team = ApiController::getBareId($from, 'teams'); $farm = new Farm(); $farmTeam = new FarmTeam(); return [AbstractEntity::STMT_FROM => "\n JOIN {$farmTeam->table('ft')} ON {$farmTeam->columnFarmId('ft')} = {$farm->columnId()}\n AND {$farmTeam->columnTeamId('ft')} = " . $farmTeam->qstr('teamId', $team) . "\n "]; } }
/** * Gets the list of the Teams which own the Farm * * @return EntityIterator|Team[] */ public function getTeams() { if (empty($this->_teams)) { $team = new Team(); $farmTeam = new FarmTeam(); $this->_teams = Team::find([AbstractEntity::STMT_FROM => " {$team->table()}\n JOIN {$farmTeam->table('ft')} ON {$farmTeam->columnTeamId('ft')} = {$team->columnId()}\n AND {$farmTeam->columnFarmId('ft')} = " . $farmTeam->qstr('farmId', $this->id) . "\n "]); } return $this->_teams; }