简体   繁体   中英

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() ?

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()?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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