public function createOrUpdate(Boat $boat, Race $race, LoggerInterface $logger) { /** @var Team $dbItem */ $dbItem = null; // first try to find by ID if (!empty($boat->id)) { $dbItem = $this->findOneByDrvId($boat->id); } // if this does not help, do a fuzzy search if (null == $dbItem) { // TODO search by club + representative + name + race? } if (null != $dbItem) { // TODO updating $logger->warning("Implementation missing for updating teams in TeamRepository::createOrUpdate"); } else { // create new team $em = $this->getEntityManager(); $dbItem = new Team(); /** @var \AppBundle\Entity\Club $club */ $club = $this->getEntityManager()->getRepository('AppBundle:Club')->findOneByDrvId($boat->club_id); if (null == $club) { $message = "Found no club with DRV-ID {$boat->club_id}! No team created for " . "[{$boat->name}, {$boat->id}]"; $logger->warning($message); throw new \Exception($message); } if ($race->getSections()->isEmpty()) { /** @var \AppBundle\Repository\RaceRepository $raceRepo */ $raceRepo = $this->getEntityManager()->getRepository('AppBundle:Race'); // create initial section $raceRepo->createSection($race, 1, $logger); } /** @var \AppBundle\Entity\RaceSection $raceSection */ $raceSection = $race->getSections()->last(); if (null == $raceSection) { $message = "Found no section for race {$race->getId()}! No team created for " . "[{$boat->name}, {$boat->id}]"; $logger->warning($message); throw new \Exception($message); } // save to DB - bugfix: lane for section is always 1 (because section does not exist yet) $em->flush(); $dbItem->setClub($club)->setDrvId($boat->id)->setName($boat->name); $em->persist($dbItem); /** @var \AppBundle\Repository\RegistrationRepository $regRepo */ $regRepo = $this->getEntityManager()->getRepository('AppBundle:Registration'); $section = new Registration(); $section->setSection($raceSection)->setLane($regRepo->getNextLaneForSection($raceSection))->setTeam($dbItem); $em->persist($section); } return $dbItem; }
public function changeRace(Registration $registration, Race $fromRace, Race $toRace) { if (is_null($registration)) { throw new \InvalidArgumentException('Registration must not be NULL'); } elseif (is_null($fromRace)) { throw new \InvalidArgumentException('Source race must not be NULL'); } elseif (is_null($toRace)) { throw new \InvalidArgumentException('Target race must not be NULL'); } /** @var EntityManager $em */ $em = $this->getEntityManager(); /** @var \AppBundle\Repository\RaceRepository $raceRepo */ $raceRepo = $em->getRepository('AppBundle:Race'); /** @var RaceSection $section */ $section = $raceRepo->getNextAvailableSection($toRace); // mark current team registration as changed and save the new race id $registration->setChangedTo($toRace); // create new registration for this team in target race $newReg = new Registration(); $newReg->setSection($section)->setLane($this->getNextLaneForSection($section))->setTeam($registration->getTeam())->setChangedFrom($fromRace); $em->persist($registration); $em->persist($newReg); $em->flush(); }