Skip to content

Instantly share code, notes, and snippets.

@frodosghost
Last active December 11, 2015 20:19

Revisions

  1. frodosghost revised this gist Jan 28, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion ProductController.php
    Original file line number Diff line number Diff line change
    @@ -58,7 +58,7 @@ public function updateAction(Request $request, $id)
    $commentForm->bind($request);

    if ($commentForm->isValid()) {
    $em->persist($entity);
    $em->persist($comment);
    $em->flush();

    return $this->redirect($this->generateUrl('product', array('id' => $id)));
  2. frodosghost revised this gist Jan 28, 2013. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions ProductController.php
    Original file line number Diff line number Diff line change
    @@ -2,6 +2,9 @@

    namespace Your\Product\Bundle\Controller\ProductController;

    use Your\Product\Bundle\Entity\Comment;
    use Your\Product\Bundle\Form\CommentType;

    class ProductController extends Controller
    {
    /**
  3. frodosghost created this gist Jan 28, 2013.
    22 changes: 22 additions & 0 deletions CommentType.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    <?php

    namespace Your\Product\Bundle\Form\CommentType;

    use Your\Product\Bundle\Form\UserType;

    class CommentType extends AbstractType
    {
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
    $builder
    ->add('comment', 'textarea', array(
    'label' => 'Comments'
    ))
    ->add('user', new UserType(), array(
    'data_class' => 'Your\Product\Bundle\Entity\User',
    ))
    ;
    }

    // ....
    }
    70 changes: 70 additions & 0 deletions ProductController.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,70 @@
    <?php

    namespace Your\Product\Bundle\Controller\ProductController;

    class ProductController extends Controller
    {
    /**
    * Show the Product and display the Comment form
    *
    * @Route("/{id}/product", name="product")
    * @Method("GET")
    * @Template()
    */
    public function productAction($id)
    {
    $em = $this->getDoctrine()->getManager();

    $product = $em->getRepository('YourProductBundle:Product')->find($id);

    if (!$product) {
    throw $this->createNotFoundException('Unable to find Product entity.');
    }

    $comment = new Comment;
    $comment->addProduct($product);
    $commentForm = $this->createForm(new CommentType(), $comment);

    return array(
    'product' => $product,
    'comment_form' => $commentForm->createView()
    );
    }

    /**
    * Adds a comment to a product
    *
    * @Route("/{id}/product/comment", name="product_add_comment")
    * @Method("PUT")
    * @Template("YourProductBundle:Product:product.html.twig")
    */
    public function updateAction(Request $request, $id)
    {
    $em = $this->getDoctrine()->getManager();

    $product = $em->getRepository('YourProductBundle:Product')->find($id);

    if (!$product) {
    throw $this->createNotFoundException('Unable to find Product entity.');
    }

    $comment = new Comment;
    $comment->addProduct($product);
    $commentForm = $this->createForm(new CommentType(), $comment);

    $commentForm->bind($request);

    if ($commentForm->isValid()) {
    $em->persist($entity);
    $em->flush();

    return $this->redirect($this->generateUrl('product', array('id' => $id)));
    }

    return array(
    'product' => $product,
    'comment_form' => $commentForm->createView()
    );
    }

    }
    20 changes: 20 additions & 0 deletions UserType.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    <?php

    namespace Your\Product\Bundle\Form\UserType;

    class UserType extends AbstractType
    {
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
    $builder
    ->add('name', 'text', array(
    'label' => 'Name'
    ))
    ->add('email', 'email', array(
    'label' => 'Email Address'
    ))
    ;
    }

    // ....
    }