この質問は SO で何度か尋ねられました - そして毎回、解決策は異なっているように見え、ほとんどの場合、答えは偶然に発見されたように見えます/またはそれは大雑把なハックです - つまり、一貫した見解がないことを意味します問題の原因とその解決方法について説明します。
「選択肢フィールドに渡されたエンティティは管理する必要があります」というメッセージも表示されますが、この例外が発生する理由が明確ではありません。
バンドルに定義された Contact クラス (.yml 形式) があります。Promotion
Contact クラスには、他の 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
上記のコントローラーコードを実行するルートを参照すると、次のエラーが表示されます。