John Davidson

php - Parent relation show in Easyadmin AssociationField

0 comments
Message:


How can I show parent categorys as Sub-options?
























































id parent_id name created_at modified_at deleted_at
1 NULL Elektronik 2022-08-06 11:36:00 2022-08-04 11:36:00 2022-08-05 11:36:00
2 1 Telefon 2022-08-05 11:36:00 2022-08-04 11:36:00 2022-08-05 11:36:00
3 NULL Vasıta 2022-08-11 11:38:00 2022-08-13 11:38:00 2022-08-04 11:38:00
4 1 Beyaz Eşya 2022-08-20 11:43:00 2022-08-05 11:43:00 2022-08-12 11:43:00
5 3 Far 2022-08-09 11:44:00 2022-08-05 11:44:00 2022-08-04 11:44:00


My Answer:
To show parent categories as sub-options in EasyAdmin using the AssociationField, you can achieve this by setting up a custom query in the EntityType field configuration.

Here's an example of how you can set up the AssociationField in your EasyAdmin configuration file:

php
public function configureFields(string $pageName): iterable
{
return [
// Other fields...
AssociationField::new('parent', 'Parent Category')
->setFormTypeOptions([
'query_builder' => function (EntityRepository $er) {
return $er->createQueryBuilder('c')
->andWhere('c.parent IS NULL')
->orderBy('c.name', 'ASC');
},
]),
];
}


In this example, we are setting up a custom query in the `query_builder` option of the `setFormTypeOptions` method for the AssociationField. The query filters out categories that have a parent (i.e., sub-categories) by checking if the `parent` field is NULL. It then orders the categories by name in ascending order.

This configuration will display the parent categories as sub-options in the AssociationField in EasyAdmin. You can adjust the query as needed to fit your specific requirements.

Rate this post

5 of 5 based on 9496 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us