Reputation: 357
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.
Upvotes: 1
Views: 2092
Reputation: 531
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 .
Upvotes: 2
Reputation: 2745
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.
Upvotes: 0