Last active
December 11, 2015 20:19
-
-
Save frodosghost/4654901 to your computer and use it in GitHub Desktop.
Base form code for saving multiple classes of data with Symfony2. This is for Jerome.
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 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', | |
)) | |
; | |
} | |
// .... | |
} |
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 Your\Product\Bundle\Controller\ProductController; | |
use Your\Product\Bundle\Entity\Comment; | |
use Your\Product\Bundle\Form\CommentType; | |
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($comment); | |
$em->flush(); | |
return $this->redirect($this->generateUrl('product', array('id' => $id))); | |
} | |
return array( | |
'product' => $product, | |
'comment_form' => $commentForm->createView() | |
); | |
} | |
} |
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 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' | |
)) | |
; | |
} | |
// .... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment