0

pu a multiplie => false を多対多関係にしようとしましたが、このエラーが発生しました。

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 は a を待ちますPlop\PlipBundle\Form\Collection

4

1 に答える 1

2

nullフィールドが空または選択されていない可能性があるため、変換関数を accept に変更する必要があります。

->prependNormTransformer(new CallbackTransformer(
    // transform the collection to its first element
    function (Collection $coll = null) {
        return $coll ? $coll[0] : null; 
    },
    // transform the element to a collection
    function (MyEntity $entity = null) {
        return new ArrayCollection($entity ? array($entity) : array());
    }
))

その後、コードは問題なく動作するはずです。

于 2012-08-09T20:12:37.673 に答える