[SOLVED] Form collection

New topics about Symfony 2 should go here

Moderators: tiagojsag, dcobalt

[SOLVED] Form collection

Postby Manu13 » Wed Jun 29, 2011 12:54 pm

Hi,

I have two entities : Product and Picture with a one-to-many relation. I try to create a product form with a pictures collection. I can edit my product and all its pictures. To add a new one, I create new fields with javascript.

The problem is that when I submit the form, new pictures are persisted without a product_id, and an exception is thrown.

What am I doing wrong ? Here is my code :

Product class
Code: Select all
    /**
     * @var ArrayCollection
     *
     * @ORM\OneToMany(targetEntity="Picture", mappedBy="product", cascade={"all"})
     */
    protected $pictures;


Picture class
Code: Select all
    /**
     * @var Product $product
     *
     * @ORM\ManyToOne(targetEntity="Product", inversedBy="pictures")
     * @ORM\JoinColumn(nullable=false)
     */
    protected $product;


Form (in my action)
Code: Select all
$product = $this->getRepository()->find(1);

$form = $this->createFormBuilder($product)
    ->add('title', 'text')
    ->add('pictures', 'collection', array(
        'type' => new PictureType(),
        'allow_add' => true,
        'prototype' => true,
    ))
    ->getForm();

$request = $this->get('request');
if ($request->getMethod() == 'POST') {
    $form->bindRequest($request);

    if ($form->isValid()) {
        $em = $this->getDoctrine()->getEntityManager();
        $em->persist($product);
        $em->flush();
    }
}
Last edited by Manu13 on Fri Jul 01, 2011 5:53 pm, edited 1 time in total.
Manu13
Junior Member
 
Posts: 19
Joined: Tue Sep 21, 2010 5:04 pm

Re: Form collection

Postby blauwers » Fri Jul 01, 2011 5:47 pm

You are having a common problem with bi-directional doctrine relationships.

bindRequest does not use class methods to put data into your original object. The net result is that your method that would otherwise set up the reverse pointer is not called. Since the reverse pointer is how doctrine figures out the relationship it causes this problem.

You need to add something like:
Code: Select all
// hack to work around bindRequest not using class methods to populate data
foreach ( $product->getPictures() as $pic ) {
         $pic->setProduct($product);
}


before your persist call to work around the problem.
blauwers
Senior Member
 
Posts: 102
Joined: Thu May 26, 2011 11:01 pm
Location: Austin, TX

Re: Form collection

Postby Manu13 » Fri Jul 01, 2011 5:53 pm

Thank you very much !
Manu13
Junior Member
 
Posts: 19
Joined: Tue Sep 21, 2010 5:04 pm

Re: [SOLVED] Form collection

Postby NOP-erator » Mon Jul 04, 2011 4:44 pm

Hey, this fixed my problems, too. Now I can add and update sub entities. But how to remove sub entities? I remove the complete sub entity from the DOM using javascript, so it isn't sent via POST. How to tell doctrine to delete the rows with data, that were not passed?

Can you please help?
NOP-erator
Junior Member
 
Posts: 18
Joined: Thu May 19, 2011 6:33 pm

Re: [SOLVED] Form collection

Postby Manu13 » Mon Jul 04, 2011 4:49 pm

I think you have to add - for example - checkboxes near from each sub entity, and after the form submission, for each of them do $em->remove($subEntity).
Manu13
Junior Member
 
Posts: 19
Joined: Tue Sep 21, 2010 5:04 pm

Re: [SOLVED] Form collection

Postby NOP-erator » Tue Jul 05, 2011 9:19 am

Manu13 wrote:I think you have to add - for example - checkboxes near from each sub entity, and after the form submission, for each of them do $em->remove($subEntity).


Unfortunately this is not an option for me. There has to be a way, I just don't know it. Actually I thought bind() in combination with doctrine is taking care of this itself.
Any other ideas?
NOP-erator
Junior Member
 
Posts: 18
Joined: Thu May 19, 2011 6:33 pm

Re: [SOLVED] Form collection

Postby maker » Tue Jul 05, 2011 11:16 am

There two approaches to deal with this:
1. After validating the form iterate trough the picture collection (if it exist) and delete all pictures which primary keys aren't submitted in your request.
2. Create additional hidden fields in your form to store the current picture collection primary key list. After validating the form make a diff between the request data of the hidden picture collection and the modified picture collection. Every key from the hidden picture collection that do not exist in the modified picture collection must be deleted.
maker
Member
 
Posts: 50
Joined: Sat Jun 13, 2009 1:19 pm
Location: Poland


Return to General Symfony 2 discussion

Who is online

Users browsing this forum: No registered users and 5 guests