/**
  * Update the number of comment for a comment
  *
  * @param null|\Sonata\NewsBundle\Model\PostInterface $post
  * @return void
  */
 public function updateCommentsCount(PostInterface $post = null)
 {
     $commentTableName = $this->em->getClassMetadata($this->getClass())->table['name'];
     $postTableName = $this->em->getClassMetadata($this->postManager->getClass())->table['name'];
     $this->em->getConnection()->query(sprintf('UPDATE %s p, (SELECT c.post_id, count(*) as total FROM %s as c WHERE c.status = 1 GROUP BY c.post_id) as count_comment
         SET p.comments_count = count_comment.total
         WHERE p.id = count_comment.post_id', $postTableName, $commentTableName));
 }
 /**
  * {@inheritdoc}
  */
 public function execute(BlockContextInterface $blockContext, Response $response = null)
 {
     $criteria = array('mode' => $blockContext->getSetting('mode'));
     $parameters = array('context' => $blockContext, 'settings' => $blockContext->getSettings(), 'block' => $blockContext->getBlock(), 'pager' => $this->manager->getPager($criteria, 1, $blockContext->getSetting('number')), 'admin_pool' => $this->adminPool);
     if ($blockContext->getSetting('mode') === 'admin') {
         return $this->renderPrivateResponse($blockContext->getTemplate(), $parameters, $response);
     }
     return $this->renderResponse($blockContext->getTemplate(), $parameters, $response);
 }
Beispiel #3
0
 /**
  * Retrieves post with id $id or throws an exception if it doesn't exist
  *
  * @param $id
  *
  * @return Post
  * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
  */
 protected function getPost($id)
 {
     $post = $this->postManager->findOneBy(array('id' => $id));
     if (null === $post) {
         throw new NotFoundHttpException(sprintf('Post (%d) not found', $id));
     }
     return $post;
 }
 /**
  * Write a post, this method is used by both POST and PUT action methods.
  *
  * @param Request  $request Symfony request
  * @param int|null $id      A post identifier
  *
  * @return FormInterface
  */
 protected function handleWritePost($request, $id = null)
 {
     $post = $id ? $this->getPost($id) : null;
     $form = $this->formFactory->createNamed(null, 'sonata_news_api_form_post', $post, array('csrf_protection' => false));
     $form->bind($request);
     if ($form->isValid()) {
         $post = $form->getData();
         $post->setContent($this->formatterPool->transform($post->getContentFormatter(), $post->getRawContent()));
         $this->postManager->save($post);
         $view = \FOS\RestBundle\View\View::create($post);
         $serializationContext = SerializationContext::create();
         $serializationContext->setGroups(array('sonata_api_read'));
         $serializationContext->enableMaxDepthChecks();
         $view->setSerializationContext($serializationContext);
         return $view;
     }
     return $form;
 }