我想我一定错过了什么 . 使用Symfony 3.4我试图设置实体的选择类型表单字段 . 具体而言,该表格用于“ property ”,其具有从人员列表中选择的所有者 . 使用Symfony最令人沮丧的方面肯定是尝试自定义表单中的列表 .

我有这个添加到构建器:

->add('owner', EntityType::class, array(
                'class' => 'AppBundle\Entity\People',
                'label' => 'Owner',
                'query_builder' => function(BusinessRepository $repo) {
                    return $repo->createBusinessPeopleQueryBuilder();
                }))

我在该FormType类文件中指定存储库:

use AppBundle\Repository\BusinessRepository;

但是,当访问路由时,我收到错误:

Type error: Argument 1 passed to 
AppBundle\Form\PropertyFormType::AppBundle\Form\{closure}() must be an 
instance of AppBundle\Repository\BusinessRepository, instance of 
octrine\ORM\EntityRepository given

研究表明,问题是BusinessRepository缺少使用声明,但肯定存在 . 函数是否存在于存储库中是没有区别的,因为它永远不会到达 . 无论使用或不使用该语句,我都会得到同样的错误 .

有什么建议?这变得非常令人沮丧 . 我还尝试通过传入一组人作为选项来获取此列表,但是通过这种方法,我收到了有关传递给选择字段的非托管实体的错误 . 没有运气可以想出那一个 .

更新:我重新编写代码,以便它不会调用存储库中的函数:

->add('owner', EntityType::class, array(
                'class' => 'AppBundle\Entity\People',
                'label' => 'Owner',
                'query_builder' => function(EntityRepository $em) use ($options) {
                    $qb = $em->createQueryBuilder('people');

                    $qb->select('p')
                       ->from('AppBundle\Entity\People', 'p')
                       ->from('AppBundle\Entity\Business', 'b')
                       ->where('p.business = b')
                       ->where('p.business = :business')
                       ->orderBy('p.last_name', 'ASC')
                       ->setParameter('business',$options['business']);
                    return $qb;
                }))

这可以检索人员列表 . 但是,现在我回到了同样的错误:

Entities passed to the choice field must be managed. Maybe persist them in 
the entity manager

解决:在我的控制器中,我正在为所有者创建一个新的People并将其传递给表单 . 删除新People实体的创建工作 . 呼 .