public function setForLine(Line $line, $endpoint, $times, $key = null) { $qb = $this->getQueryBuilder(self::SUBSCRIPTION_ENTITY); $lineEntity = $this->entityManager->find('TubeService:Line', $line->getId()); // start a transaction $this->entityManager->getConnection()->beginTransaction(); try { // set all previous subscriptions for this line and endpoint to inactive $q = $qb->update()->set(self::TBL . '.is_active', '0')->where(self::TBL . '.line = ?1', self::TBL . '.endpoint = ?2')->setParameter(1, $line->getId()->getId())->setParameter(2, $endpoint)->getQuery(); $q->execute(); // create the domain entities for each time block foreach ($times as $day => $groups) { foreach ($groups as $group) { $subscription = new Subscription(); $subscription->setLine($lineEntity); $subscription->setDay($day); $subscription->setEndpoint($endpoint); $subscription->setPublicKey($key); $subscription->setStartHour($group->start); $subscription->setEndHour($group->end); $this->entityManager->persist($subscription); } } // commit the transaction $this->entityManager->flush(); $this->entityManager->getConnection()->commit(); return true; } catch (Exception $e) { $this->entityManager->getConnection()->rollback(); throw $e; } }
public function addNewStatus(Line $line, TFLLine $TFLLine) { // get the original lineEntity out of the database $lineEntity = $this->entityManager->find('TubeService:Line', $line->getId()); // create a new status for this line $status = new StatusDb(); $status->setLine($lineEntity); $description = null; $descriptions = $TFLLine->getStatusDescriptions(); if ($descriptions) { $description = implode('|', $descriptions); } $status->setDescription($description); $status->setIsDisrupted($TFLLine->isDisrupted()); $status->setTitle($TFLLine->getStatusTitle()); $status->setShortTitle($TFLLine->getStatusShortTitle()); $this->entityManager->persist($status); $this->entityManager->flush(); // use the new status ID to update the line $lineEntity->setLatestStatus($status); $this->entityManager->persist($lineEntity); $this->entityManager->flush(); return $status; }