Created
May 26, 2012 23:39
-
-
Save enricostano/2795632 to your computer and use it in GitHub Desktop.
Term Controller
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace StanoSas\Bundle\TaxonomyBundle\Controller; | |
use Symfony\Bundle\FrameworkBundle\Controller\Controller; | |
use StanoSas\Bundle\TaxonomyBundle\Entity\Term; | |
use StanoSas\Bundle\TaxonomyBundle\Form\TermType; | |
class TermController extends Controller | |
{ | |
public function newAction($vid) | |
{ | |
$vocabulary = $this->getVocabulary($vid); | |
$term = new Term(); | |
$term->setVocabulary($vocabulary); | |
$form = $this->createForm(new TermType(), $term); | |
return $this->render('StanoSasTaxonomyBundle:Term:form.html.twig', array( | |
'term' => $term, | |
'form' => $form->createView() | |
)); | |
} | |
public function createAction($vid) | |
{ | |
$vocabulary = $this->getVocabulary($vid); | |
$term = new Term(); | |
$term->setVocabulary($vocabulary); | |
$request = $this->getRequest(); | |
$form = $this->createForm(new TermType(), $term); | |
$form->bindRequest($request); | |
if ($form->isValid()) { | |
$em = $this->getDoctrine() | |
->getEntityManager(); | |
$parent = $this->getDoctrine()->getRepository('\StanoSas\Bundle\TaxonomyBundle\Entity\Term')->find(2); | |
$term->setParent($parent); | |
$em->persist($term); | |
$em->flush(); | |
return $this->redirect($this->generateUrl('StanoSasTaxonomyBundle_vocabulary_show', array( | |
'vid' => $vid))); | |
} | |
return $this->render('StanoSasTaxonomyBundle:Term:create.html.twig', array( | |
'term' => $term, | |
'form' => $form->createView() | |
)); | |
} | |
protected function getVocabulary($vid) | |
{ | |
$em = $this->getDoctrine() | |
->getEntityManager(); | |
$vocabulary = $em->getRepository('StanoSasTaxonomyBundle:Vocabulary')->find($vid); | |
if (!$vocabulary) { | |
throw $this->createNotFoundException('Unable to find Vocabulary.'); | |
} | |
return $vocabulary; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment