Example #1
0
File: Farm.php Project: scalr/scalr
 /**
  * Associates a Farm with specified Teams.
  * 
  * This method should only be called within a transaction.
  *
  * @param   EntityIterator|Team[]   $teams    Collection of the Teams
  */
 public function saveTeams($teams)
 {
     if (!$this->db()->transCnt) {
         throw new LogicException("This method should only be called within a transaction!");
     }
     $farmTeam = new FarmTeam();
     if (!empty($teams)) {
         $values = [];
         $args = [];
         $farmIdType = $farmTeam->getIterator()->getField('farmId')->type;
         $farmIdWh = $farmIdType->wh();
         $teamIdType = $farmTeam->getIterator()->getField('teamId')->type;
         $teamIdWh = $teamIdType->wh();
         foreach ($teams as $team) {
             $values[] = "({$farmIdWh}, {$teamIdWh})";
             $args[] = $farmIdType->toDb($this->id);
             $args[] = $teamIdType->toDb($team->id);
         }
     }
     FarmTeam::deleteByFarmId($this->id);
     if (!empty($values)) {
         $this->db()->Execute("INSERT IGNORE INTO {$farmTeam->table()} (`farm_id`, `team_id`) VALUES " . implode(', ', $values), $args);
     }
 }