[英]Using a Custom Query for the Entities in Doctrine - Symfony Form
I have two tables one is users like this 我有两个表,一个是这样的用户
id _ username _ firstname _ lastname ID _用户名_名_姓
and the other one is users_test 另一个是users_test
id _ user_id _ name ID _用户ID _名称
I'm trying in Symfony Form (UserTestType) to make a Custom Query to get the id from the first Entitiy (UsersType) and insert the id as a Value. 我正在尝试在Symfony表单(UserTestType)中进行自定义查询,以从第一个实体(UsersType)获取ID并将ID作为值插入。 my code is like this:
我的代码是这样的:
$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',
));
when i reload the page it says: Could not load type "Symfony\\Component\\Form\\Extension\\Core\\Type\\EntityType" 当我重新加载页面时,它说:无法加载类型“ Symfony \\ Component \\ Form \\ Extension \\ Core \\ Type \\ EntityType”
note: I'v used Symfony\\Component\\Form\\Extension\\Core\\Type\\EntityType; 注意:我使用的是Symfony \\ Component \\ Form \\ Extension \\ Core \\ Type \\ EntityType;
I hope someone could help me to find my Problem. 我希望有人可以帮助我找到我的问题。
I just tried it out and you probally copy&paste the TextType from user/first/lastname and just changed the last part of the Namespace, which lead you to the namespace where TextType and many other Types are lying. 我刚刚尝试了一下,您可能从用户/名字/姓氏中复制并粘贴了TextType,并刚刚更改了命名空间的最后一部分,这将导致您进入TextType和许多其他类型所在的命名空间。 (I did the exact same mistake and lost 2 minutes of my life)
(我犯了完全相同的错误,失去了生命的2分钟)
For EntityType and DoctrineType, you need to use following namespace: 对于EntityType和DoctrineType,您需要使用以下命名空间:
use Symfony\\Bridge\\Doctrine\\Form\\Type\\EntityType;
Also if you didn't do it (I know you wrote you use it, but just in case), you should also use your User Entity Class like: 另外,如果您没有这样做(我知道您曾使用过它,但是以防万一),还应该使用User Entity Class,例如:
use UserBundle\\Entity\\Users;
(or using the Singular form User
, which I would recommend so you have no struggling with singular/plural) (或使用单数形式的
User
,我建议这样做,这样您就不会为单数/复数而苦恼)
I also remember reading somewhere that it is recommended to use the FQCN via ::class
so you could think changing: 我还记得在某处读过,建议通过
::class
使用FQCN,以便您可以考虑更改:
'class' => 'UserBundle:Users',
to 至
'class' => Users::class,
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.