1

这个问题已经在 SO 上被问过好几次了——每次,解决方案似乎都不同,而且几乎总是,答案似乎是偶然发现的/或者它是一个粗略的 hack——这意味着没有一致的观点导致问题的原因以及如何解决问题。

我还遇到消息“必须管理传递给选择字段的实体” - 目前尚不清楚为什么会引发此异常。

我在捆绑包中定义了一个联系人类(.yml 格式)。Contact 类与其他两个类具有 manyToOne 关系,Promotion并且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。在创建表单之前尝试持久化promotionreferrer实体

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 回答