1

この質問は SO で何度か尋ねられました - そして毎回、解決策は異なっているように見え、ほとんどの場合、答えは偶然に発見されたように見えます/またはそれは大雑把なハックです - つまり、一貫した見解がないことを意味します問題の原因とその解決方法について説明します。

「選択肢フィールドに渡されたエンティティは管理する必要があります」というメッセージも表示されますが、この例外が発生する理由が明確ではありません。

バンドルに定義された Contact クラス (.yml 形式) があります。PromotionContact クラスには、他の 2 つのクラスとの manyToOne 関係がありContactReferrerます。以下を参照してください。

Foobar\ContactlistBundle\Entity\Contact:
    type: entity
    table: contact
    repositoryClass: Foobar\ContactlistBundle\Repository\ContactRepository

    id:
        id:
            type: integer
            generator: { strategy: AUTO }
    fields:
        first_name:
            type: string
            length: 32
            nullable: true

        last_name:
            type: string
            length: 64
            nullable: true

        email:
            type: string
            length: 128
            unique: true

        token:
            type: string
            length: 8
            unique: true

        is_validated:
            type: boolean

        created_at:
            type: datetime

        updated_at:
            type: datetime
            nullable: true


    manyToOne:
        promotion:
            targetEntity: Promotion
            inversedBy: promoted_contacts
            joinColumn:
                name: promotion_id
                referencedColumnName: id

        referrer:
            targetEntity: ContactReferrer
            inversedBy: referrer_contacts
            joinColumn:
                name: contact_referrer_id
                referencedColumnName: id

フォームを使用して生成し、php app/console doctrine:generate:form FooBarContaclistBundleContact次のようにフォームを手動で編集しました。

class ContactType extends AbstractType
{
    public function buildForm(FormBuilder $builder, array $options)
    {
        $builder
            ->add('first_name')
            ->add('last_name')
            ->add('email', 'email')
            ->add('token')
            ->add('is_validated')
            ->add('created_at')
            ->add('updated_at')
            ->add('promotion', 'entity', array(
                                                'class' => 'Foobar\ContactlistBundle\Entity\Promotion',
                                                'expanded' => false,
                                                'multiple' => false, )
                 )
            ->add('referrer', 'entity', array(
                                                'class' => 'Foobar\ContactlistBundle\Entity\ContactReferrer',
                                                'expanded' => false,
                                                'multiple' => false, )
            );
    }

    public function getName()
    {
        return 'Foobar_contactlistbundle_contacttype';
    }

    public function getDefaultOptions(array $options)
    {
        return array(
            'data_class' => 'Foobar\ContactlistBundle\Entity\Contact',
        );
    }
}

私の Entity/Contact.php クラスでは、コンストラクターのコードは次のとおりです。

class Contact
{
    public function __construct()
    {
        $this->token = 'abcdef123';
        $this->is_validated = false;
        $this->created_at =  new \DateTime();
        $this->setUpdatedAt(new \DateTime());

        // Set these to defaults
        $this->promotion = new \Foobar\ContactlistBundle\Entity\Promotion();
        $this->referrer = new \Foobar\ContactlistBundle\Entity\ContactReferrer();
    }

    // more code ... 
}

私のコントローラーには、次のコードがあります。

public function newcontactAction(Request $request)
{
    // do something ...
    $contact = new Contact();
    $form = $this->createForm(new ContactType(), $contact);  // <- Exception thrown here
    // do something ...
}

Entities passed to the choice field must be managed上記のコントローラーコードを実行するルートを参照すると、次のエラーが表示されます

4

1 に答える 1

5

Entities passed to the choice field must be managed関連するエンティティを entityManager に永続化する必要があることを意味します。フォームを作成する前にエンティティpromotionとエンティティを永続化してくださいreferrer

public function newcontactAction(Request $request)
{
    // do something ...
    $em->persist($contact->getPromotion());
    $em->persist($contact->getReferrer());
    $form = $this->createForm(new ContactType(), $contact);

これで問題が解決しない場合は、新しいプロモーション エンティティとリファラー エンティティを作成し、それらを entityManager に永続化する必要がありflush()ます。この手順の後でのみ、新しい連絡先エンティティを作成し、フォームを作成します。

于 2012-08-21T14:16:00.147 に答える