![](/img/trans.png)
[英]Symfony2/Doctrine - form using two entities and a jointable/joinentity with metadata
[英]Using a Custom Query for the Entities in Doctrine - Symfony Form
我有兩個表,一個是這樣的用戶
ID _用戶名_名_姓
另一個是users_test
ID _用戶ID _名稱
我正在嘗試在Symfony表單(UserTestType)中進行自定義查詢,以從第一個實體(UsersType)獲取ID並將ID作為值插入。 我的代碼是這樣的:
$builder->add('user_id', EntityType::class, array(
'class' => 'UserBundle:Users',
'query_builder' => function (EntityRepository $er) {
return $er->createQueryBuilder('u')
->orderBy('u.id', 'ASC');
},
'choice_label' => 'firstname',
'choice_value' => 'id',
));
當我重新加載頁面時,它說:無法加載類型“ Symfony \\ Component \\ Form \\ Extension \\ Core \\ Type \\ EntityType”
注意:我使用的是Symfony \\ Component \\ Form \\ Extension \\ Core \\ Type \\ EntityType;
我希望有人可以幫助我找到我的問題。
我剛剛嘗試了一下,您可能從用戶/名字/姓氏中復制並粘貼了TextType,並剛剛更改了命名空間的最后一部分,這將導致您進入TextType和許多其他類型所在的命名空間。 (我犯了完全相同的錯誤,失去了生命的2分鍾)
對於EntityType和DoctrineType,您需要使用以下命名空間:
use Symfony\\Bridge\\Doctrine\\Form\\Type\\EntityType;
另外,如果您沒有這樣做(我知道您曾使用過它,但是以防萬一),還應該使用User Entity Class,例如:
use UserBundle\\Entity\\Users;
(或使用單數形式的User
,我建議這樣做,這樣您就不會為單數/復數而苦惱)
我還記得在某處讀過,建議通過::class
使用FQCN,以便您可以考慮更改:
'class' => 'UserBundle:Users',
至
'class' => Users::class,
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.