/**
  * @Route("/add-user", name="subscription_add_user")
  *
  * @param Request $request
  * @return \Symfony\Component\HttpFoundation\Response
  */
 public function addUserAction(Request $request)
 {
     $subscription = new Subscription();
     $subscription->setType(Reference::USER);
     $form = $this->getEditForm($subscription);
     if ($this->processForm($form, $request)) {
         return $this->redirect($this->generateUrl('subscription_show', ['id' => $subscription->getId()]));
     }
     return $this->render('subscription/edit.html.twig', ['subscription' => $subscription, 'form' => $form->createView()]);
 }
 public function unsubscribe(User $user, Subscription $subscription)
 {
     $query = $this->getEntityManager()->createNativeQuery('DELETE FROM xdb__user_subscription_relation WHERE subscription_id=? AND user_id=?', new ResultSetMapping());
     $query->setParameter(1, $subscription->getId());
     $query->setParameter(2, $user->getId());
     try {
         $query->getResult();
     } catch (\PDOException $e) {
         // this is thrown, but nothing went wrong (seems like)
     }
 }
 public function setTokens(User &$user, array $tokens)
 {
     $em = $this->getEntityManager();
     $userRepo = $em->getRepository('AppBundle:User');
     foreach ($tokens as $token) {
         $subscription = $this->findOneByToken($token);
         if (!$subscription) {
             $subscription = new Subscription();
             $subscription->setToken($token);
             $em->persist($subscription);
             $em->flush();
         }
         $userRepo->subscribe($user, $subscription);
     }
 }
 /**
  * @Route("/add-subscription", name="add_subscription")
  */
 public function addSubscription(Request $request)
 {
     $em = $this->getDoctrine()->getManager();
     $stage = $em->getRepository('AppBundle:Stage')->findOneById($request->get('stage'));
     if ($stage != null) {
         $sub = new Subscription();
         $sub->setStage($stage);
         $sub->setSubscribed(true);
         $em->persist($sub);
         $em->flush();
     }
     $this->addFlash('notice', 'Subscribed to stage ' . $stage);
     return $this->redirectToRoute('subscriptions');
 }