protected function moveInSection(Request $request, Registration $registration, $direction) { // TODO preconditions! // check if $registration is null --> error message // check if $direction has a allowed value --> error message $em = $this->getDoctrine()->getManager(); $section = $registration->getSection(); $myLane = $registration->getLane(); $myNewLane = -1; if ('up' == $direction) { $myNewLane = $myLane - 1; } elseif ('down' == $direction) { $myNewLane = $myLane + 1; } if (-1 == $myNewLane) { // TODO error message with flash and redirection die('[moveInSection] myNewLane = -1'); // TODO remove this method as soon as preconditions check exists } $other = null; // search for the team "directly besides me" /** @var Registration $team */ foreach ($section->getValidRegistrations() as $team) { if ($team->getLane() == $myNewLane) { $other = $team; break; } } // if it exists, then change the position of that // team to the one of the moving team if (!is_null($other)) { $team->setLane($myLane); $em->persist($team); } // change the position of the given team to the new one $registration->setLane($myNewLane); $em->persist($registration); // store in DB $em->flush(); $this->addFlash('notice', "Sportler verschoben ({$myLane} >> {$myNewLane})"); return $this->redirect($request->headers->get('referer')); }