Reputation: 199
I'm trying to embed collection of forms into one based on this cookbook: http://symfony.com/doc/current/cookbook/form/form_collections.html
I have following database shcema: product ---< POS shortcut >--- POS item
in controller i'm initiatig form like this:
$pos = new PosList();
for ($i=0;$i<10 ;$i++ ) {
$scut1 = new PosShortcut();
$pos->addPosShortcut($scut1);
}
$form = $this->createForm(new PosListType(), $pos);
Then i have form for POSShortcut:
$builder
->add('posList','hidden')
->add('product','entity',array(
'required'=>false,
'empty_value'=>'Choose product...',
'label'=>'Shordcut product',
'class'=>'StockBundle:Product',
'property'=>'name'
));
And of course main form, what includes PosShortcut form:
<...>
->add('posShortcut','collection',array('type'=>new PosShortcutType()))
<...>
Now the problem is that for a POS shortcut item, POSList id is set to NULL when it's written in database. Reason is very simple, i haven't set PosList value for new shortcut when i'm creating them in controller at firstplace. But the issue is that i can not do that in this state, because POS item does not exists in database yet.
How to get over this problem ?
Upvotes: 0
Views: 152
Reputation: 48893
class PosList
{
public function addPosShortcut($shortcut)
{
$this->shortcuts->add($shortcut);
$shortcut->setPosList($this); // *** This will link the objects ***
}
Doctrine 2 will take care of the rest. You should be aware that all 10 of your shortcuts will always be posted.
Upvotes: 1