Reputation: 191
I have a many-to-many relationship between two entities A and B.
So when adding a form, in order to add entityA
to entityB
, I am doing the following:
$builder
->add('entityAs', 'entity', array(
'class' => 'xxxBundle:EntityA',
'property' => 'name',
'multiple' => true,
));}
And everything is alright.
But depending on the field type of entityA, I want to sometimes set 'multiple' to false, so I'm doing the following :
if($type=='a'){
$builder
->add('entityAs', 'entity', array(
'class' => 'xxxBundle:entityA',
'property' => 'name',
'multiple' => true,
));}
else {
$builder
->add('entityAs', 'entity', array(
'class' => 'xxxBundle:entityA',
'property' => 'name',
'multiple' => false,
));
}
This gives me the following error:
Catchable Fatal Error: Argument 1 passed to Doctrine\Common\Collections\ArrayCollection::__construct() must be an array, object given, called in C:\wamp\www\Symfony\vendor\doctrine\orm\lib\Doctrine\ORM\UnitOfWork.php on line 519 and defined in C:\wamp\www\Symfony\vendor\doctrine\common\lib\Doctrine\Common\Collections\ArrayCollection.php line 48
Can anybody help me?
Upvotes: 2
Views: 2429
Reputation: 5158
In EntityA, you have something like this, right?
public function setEntitiesB($data)
{
$this->entitiesB = $data ;
}
Now because you can also receive single value instead of array of values, you need something like this:
public function setEntitiesB($data)
{
if ( is_array($data) ) {
$this->entitiesB = $data ;
} else {
$this->entitiesB->clear() ;
$this->entitiesB->add($data) ;
}
}
Upvotes: 6
Reputation: 1919
i would check the entityA value in the controller and depending on it create different forms.
in controller:
if ($entityA->getType() == 'a') {
$form = new FormB(); // form with multiple true
} else {
$form = new FormA(); // form with multiple false
}
Upvotes: 0