Skip to content

Instantly share code, notes, and snippets.

@wouterj
Created December 9, 2013 21:28
Show Gist options
  • Save wouterj/7881270 to your computer and use it in GitHub Desktop.
Save wouterj/7881270 to your computer and use it in GitHub Desktop.
<?php
namespace Wj\ForumBundle\Document;
use Doctrine\ODM\PHPCR\Mapping\Annotations as PHPCR;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @PHPCR\Document(referenceable=true)
*/
class Topic
{
/**
* @PHPCR\Id
*/
private $id;
/**
* @PHPCR\NodeName
* @Assert\NotBlank
*/
private $title;
/**
* @PHPCR\String
* @Assert\NotBlank
*/
private $content;
/**
* @PHPCR\ParentDocument
* @Assert\NotNull
*/
private $category;
/**
* @PHPCR\Children
*/
private $reactions = array();
public function setTitle($title)
{
$this->title = $title;
return $this;
}
public function getTitle()
{
return $this->title;
}
public function setContent($content)
{
$this->content = $content;
return $this;
}
public function getContent()
{
return $this->content;
}
public function setCategory(Category $category)
{
$this->category = $category;
return $this;
}
public function getCategory()
{
return $this->category;
}
public function addReaction($reaction)
{
$reaction->setTopic($this);
$this->reactions[] = $reaction;
}
public function getReactions()
{
return $this->reactions;
}
}
<?php
namespace Wj\ForumBundle\Controller;
use Wj\ForumBundle\Document\Topic;
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Session\Session;
use Doctrine\Bundle\PHPCRBundle\ManagerRegistry;
class TopicController
{
protected $templating;
protected $formFactory;
protected $doctrinePhpcr;
protected $session;
public function __construct(EngineInterface $templating, FormFactoryInterface $formFactory, ManagerRegistry $doctrinePhpcr, Session $session)
{
$this->templating = $templating;
$this->formFactory = $formFactory;
$this->doctrinePhpcr = $doctrinePhpcr;
$this->session = $session;
}
public function createAction(Request $request)
{
$topic = new Topic();
$form = $this->formFactory->createBuilder('form', $topic)
->add('title', 'text')
->add('category', 'phpcr_document', array(
'class' => 'WjForumBundle:Category',
))
->add('content', 'textarea')
->add('submit', 'submit')
->getForm();
$form->handleRequest($request);
if ($form->isValid()) {
$dm = $this->doctrinePhpcr->getManager();
$dm->persist($topic);
$dm->flush();
$this->session->getFlashBag()->add('notice', sprintf('Topic "%s" is created', $topic->getTitle()));
return new RedirectResponse('/topic/create/success');
}
return $this->templating->renderResponse('WjForumBundle:Topic:create.html.twig', array(
'form' => $form->createView(),
));
}
public function createSuccessAction()
{
return $this->templating->renderResponse('WjForumBundle:Topic:create_success.html.twig', array(
'message' => current($this->session->getFlashBag()->get('notice')),
));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment