简体   繁体   English

奏鸣曲管理员中的自定义查询

[英]Custom query in sonata admin

I'm new to sonata and symfony and I was wondering if there is a way to create a custom query for one of the entities in configureFormFileds() ? 我是Sonata和Symfony的新手,我想知道是否有一种方法可以为configureFormFileds()中的实体之一创建自定义查询?

I need it to build a rather complex admin view over several entities that are many-to-many related via a single intermediate table in a star-like fashion (many joins). 我需要它以星状的方式(通过多个联接)通过单个中间表在几个与多对多相关的实体上构建一个相当复杂的管理视图。 My idea was to build a complex query that fetches all the data and then pass it to my form. 我的想法是建立一个复杂的查询,以获取所有数据,然后将其传递给我的表单。

I've also tried to map all these relations in doctrine and fetch them one by one in a series of custom forms, but unfortunately, entities must be checked against each other, so that didn't work. 我还尝试将所有这些关系映射到学说中,并以一系列自定义形式一个个地获取它们,但是不幸的是,必须对实体进行相互检查,以至于无法正常工作。

Yes, you can create custom query for Entity. 是的,您可以为实体创建自定义查询。 (example for Doctrine) (教义示例)

       ->add(
            'manager',
            EntityType::class,
            [
                'label' => 'Manager',
                'class' => 'MainBundle\Entity\Manager',
                'query_builder' => function (EntityRepository $er) {
                    return $er->createQueryBuilder('m')
                        ->where('m.username LIKE :username')
                        ->setParameter('username', $this->getConfigurationPool()
                            ->getContainer()
                            ->get('security.token_storage')->getToken()->getUser()->getName()
                        )
                        ->orderBy('m.id', 'ASC');
                },
            ]
        )

How create query - lock at the official documentation . 如何在官方文档中创建查询锁定。

you need to provide some more information to get a precise answer, but regarding your main question: 您需要提供更多信息来获得准确的答案,但要注意您的主要问题:

I'm new to sonata and symfony and I was wondering if there is a way to create a custom query for one of the entities in configureFormFileds()? 我是Sonata和Symfony的新手,我想知道是否有一种方法可以为configureFormFileds()中的实体之一创建自定义查询?

I can answer. 我可以回答。 In general this is possible, yes. 通常,这是可能的,是的。 Check out the query config parameter of the sonata_type_model or the callback option of the sonata_type_model_autocomplete field type. 检出sonata_type_modelquery配置参数或sonata_type_model_autocomplete字段类型的callback选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM