Forms “故障排除”;传递到选择字段的实体必须进行管理";
这个问题已经被问了好几次了——每次,解决方案似乎都不同,而且几乎总是,答案似乎是偶然发现的/或者是一个粗糙的黑客——这意味着对于问题的原因以及如何解决问题没有一致的看法 我还遇到消息“传递到选择字段的实体必须进行管理”——不清楚为什么会引发此异常 我在一个包中定义了一个联系人类(.yml格式)。Contact类与另外两个类Forms “故障排除”;传递到选择字段的实体必须进行管理";,forms,symfony,Forms,Symfony,这个问题已经被问了好几次了——每次,解决方案似乎都不同,而且几乎总是,答案似乎是偶然发现的/或者是一个粗糙的黑客——这意味着对于问题的原因以及如何解决问题没有一致的看法 我还遇到消息“传递到选择字段的实体必须进行管理”——不清楚为什么会引发此异常 我在一个包中定义了一个联系人类(.yml格式)。Contact类与另外两个类Promotion和ContactReferer有多个关系-请参见以下内容: Foobar\ContactlistBundle\Entity\Contact: type
Promotion
和ContactReferer
有多个关系-请参见以下内容:
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应用程序/控制台原则: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 ...
}
公共功能newcontactAction(请求$Request)
{
//做点什么。。。
$contact=新联系人();
$form=$this->createForm(new ContactType(),$contact);//传递到选择字段的实体必须被管理
意味着必须将相关实体持久化到entityManager。在创建表单之前,请尝试持久化促销
和推荐人
实体
公共功能newcontactAction(请求$Request)
{
//做点什么。。。
$em->persist($contact->getPromotion());
$em->persist($contact->getreferer());
$form=$this->createForm(新联系人类型(),$contact);
如果这没有帮助,您将不得不创建新的促销和推荐实体,将它们持久化到entityManager,而不是执行flush()
。仅在此步骤之后创建新的联系人实体并创建表单。传递到选择字段的实体必须进行管理
意味着必须将相关实体持久化到entityManager。在创建表单之前,请尝试持久化促销
和推荐人
实体
公共功能newcontactAction(请求$Request)
{
//做点什么。。。
$em->persist($contact->getPromotion());
$em->persist($contact->getreferer());
$form=$this->createForm(新联系人类型(),$contact);
如果这对您没有帮助,您将不得不创建新的促销和推荐实体,将它们持久化到entityManager,而不是执行flush()
。只有在执行此步骤后,才能创建新的联系人实体并创建表单。我不明白Symfony为什么这样做我不明白Symfony为什么这样做