我尝试 pu a multiplie => false to a manytomany 关系,但我有这个错误。
Expected an object, but got a collection. Did you forget to pass
“multiple = true”到实体字段?
所以我找到了解决方案,但我还有其他错误。
Catchable Fatal Error: Argument 1 passed to Plop\PlipBundle\Form\{closure}() must be an instance of Plop\PlipBundle\Form\Collection, null given in /var/www/Symfony/src/plop/plipBundle/Form/CampaignSupportType.php line 22
并且是我的代码:
public function buildForm(FormBuilder $builder, array $options)
{
$builder->add(
$builder->create('supports', 'entity', array(
'class' => 'PlopPlipBundle:Support','multiple' => false,
'query_builder' => function(EntityRepository $er) {
return $er->createQueryBuilder('s')
->orderBy('s.name', 'ASC');
}, 'property' => 'name'))
->prependNormTransformer(new CallbackTransformer(
// transform the collection to its first element
function (Collection $coll) { return $coll[0]; },
// transform the element to a collection
function (MyEntity $entity) { return new ArrayCollection(array($entity)); }
))
);
}
编辑
随着@bernhard 的回答,我得到了一个新错误:
Catchable Fatal Error: Argument 1 passed to Plop\PlipBundle\Form\{closure}() must be an instance of Plop\PlipBundle\Form\Collection, instance of Doctrine\Common\Collections\ArrayCollection given in /var/www/Symfony/src/plop/PlipBundle/Form/CampaignSupportType.php line 22
现在不是 null 给定的,但是Doctrine\Common\Collections\ArrayCollection
Symfony2 等待一个Plop\PlipBundle\Form\Collection
.