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;
 }
 /**
  * Creates a new Team entity.
  *
  * @Route("/team/new", name="team_new")
  * @Method({"GET", "POST"})
  */
 public function newAction(Request $request)
 {
     /** @var EntityManager $em */
     $em = $this->getDoctrine()->getManager();
     $maxTeamSize = 9;
     $minAge = 0;
     $maxAge = 100;
     $gender = Competitor::GENDER_BOTH;
     //$withCox = true;
     /** @var Race $race */
     $race = $request->get('race', null);
     if (!is_null($race)) {
         /** @var RaceRepository $raceRepo */
         $raceRepo = $em->getRepository('AppBundle:Race');
         /** @var Race|null $race */
         $race = $raceRepo->find(intval($race));
         if (!is_null($race)) {
             $maxTeamSize = $race->getTeamsize();
             $minAge = $race->getAgeMin();
             $maxAge = $race->getAgeMax();
             $gender = $race->getGender();
         }
     }
     // get all potential candidates
     /** @var MembershipRepository $memberRepo */
     $memberRepo = $em->getRepository('AppBundle:Membership');
     $memberships = $memberRepo->findAllCurrent($gender, $minAge, $maxAge);
     if (0 == count($memberships)) {
         $this->addFlash('error', 'Keine passenden Sportler gefunden, um neue Mannschaft anzulegen!');
         return $this->redirect($request->headers->get('referer'));
     } else {
         /** @var ClubRepository $clubRepo */
         $clubRepo = $em->getRepository('AppBundle:Club');
         $clubs = $clubRepo->findAll();
         usort($clubs, function ($a, $b) {
             /** @var Club $a */
             $left = $a->getCity() . '_' . $a->getAbbreviation();
             /** @var Club $b */
             $right = $b->getCity() . '_' . $b->getAbbreviation();
             return strcmp($left, $right);
         });
         // no model
         $data = array();
         $fb = $this->createFormBuilder($data);
         $fb->add('club', ChoiceType::class, array('label' => 'Meldender Club', 'required' => true, 'expanded' => false, 'multiple' => false, 'choices' => $clubs, 'choice_label' => function ($club, $key, $index) {
             /** @var Club $club */
             return $club->getName() . ' (' . $club->getCity() . ')';
         }, 'group_by' => function ($club, $key, $index) {
             /** @var Club $club */
             return substr($club->getCity(), 0, 1);
         }));
         // team members at the positions
         for ($i = 1; $i <= $maxTeamSize; $i++) {
             $fb->add('members_' . $i, ChoiceType::class, array('label' => 'Platz ' . $i, 'required' => true, 'expanded' => false, 'multiple' => false, 'choices' => $memberships, 'choice_label' => function ($m, $key, $index) use($gender) {
                 /** @var Membership $m */
                 /** @var Competitor $p */
                 $p = $m->getPerson();
                 $result = $p->getFirstName() . ' ' . strtoupper($p->getLastName()) . ' (' . $p->getYearOfBirth() . ', ' . $p->getAge();
                 if (Competitor::GENDER_BOTH == $gender) {
                     $result .= ', ' . $p->getGenderSymbol();
                 }
                 $result .= ')';
                 return $result;
             }, 'group_by' => function ($m, $key, $index) {
                 /** @var Membership $m*/
                 if (!empty(trim($m->getClub()->getShortname()))) {
                     return $m->getClub()->getShortname();
                 } else {
                     return $m->getClub()->getName();
                 }
             }));
         }
         $form = $fb->getForm();
         $form->handleRequest($request);
         if ($form->isSubmitted()) {
             $isGood = true;
             if (!$form->isValid()) {
                 foreach ($form->getErrors() as $error) {
                     $this->addFlash('error', $error->getMessage());
                 }
                 $isGood = false;
             }
             if ($isGood) {
                 /** @var Club $club */
                 $club = $form->get('club')->getData();
                 if (is_null($club)) {
                     $this->addFlash('error', 'Kein Club angegeben!');
                     $isGood = false;
                 }
             }
             $team = null;
             if ($isGood) {
                 $team = new Team();
                 $team->setClub($club);
                 $team->setName($club->getName());
                 $em->persist($team);
             }
             $found[] = array();
             for ($i = 1; $i <= $maxTeamSize; $i++) {
                 /** @var Membership $m */
                 $m = $form->get('members_' . $i)->getData();
                 if (is_null($m)) {
                     $this->addFlash('error', sprintf("Kein Sportler an Position %d angegeben!", $i));
                     $isGood = false;
                 } else {
                     if (in_array($m->getId(), $found)) {
                         $this->addFlash('error', sprintf("Sportler an Position %d mehrfach angegeben!", $i));
                         $isGood = false;
                     } else {
                         $found[] = $m->getId();
                         $posInTeam = new TeamPosition();
                         $posInTeam->setTeam($team)->setPosition($i)->setIsCox(false)->setMembership($m);
                         $em->persist($posInTeam);
                     }
                 }
             }
             if ($isGood) {
                 $em->flush();
                 $this->addFlash('notice', 'Mannschaft erfolgreich angelegt!');
             }
         }
         return $this->render('team/new.html.twig', array('race' => $race, 'form' => $form->createView()));
     }
 }