Reputation: 63
I'm working on an application builded with Symfony 7.1 and EasyAdmin 4.10 with 3 Entities: Mission, SkillCategory and Skill The relationship between Mission and Skills is ManyToMany I just want to be able to add a SkillField into my MissionCrudController, BUT, i want to be able to specify the order_by of each Skill, the way i can with the Symfony ChoiceType, i tried with this : In my MissionCrudController :
$skillsField = ChoiceField::new('skills', t('ui.mission.skills'))
->setFormType(SkillType::class);
$categories = $this->skillCategoryRepository->findAll();
$choices = [];
foreach ($categories as $category) {
$skills = $category->getSkills();
if ($skills) {
$group = [];
/** @var Skill $skill */
foreach ($skills as $skill) {
$group[$skill->getName()] = $skill->getId();
}
$choices[$category->getName()] = $group;
}
}
return $choices;
});
Or with this code :
$skillsField = ChoiceField::new('skills', t('ui.mission.skills'))
->allowMultipleChoices(true)
->setFormType(SkillType::class)
;
With the following SkillTypeField :
final class SkillType extends AbstractType
{
public function __construct(private readonly SkillRepository $skillRepository)
{
}
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('name', ChoiceType::class,
[
'group_by' => 'category',
'choice_value' => 'id',
'choices' => $this->skillRepository->findAll(),
'choice_label' => fn(Skill $skill) => $skill->getName(),
'multiple' => true,
'data_class' => Skill::class
]
);
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'data_class' => Skill::class
]);
}
}
But i keep getting different errors, i think what i'm trying to do is somehow kind of basic, so i must be doing something wrong,...
If anyone can give me a helpful hand, that would be really great, cause i'm really stuck, if the way to do so is to use AssociationField, then, how to do so with the group_by parameter ?!!
I tried to get a ChoiceField with easyAdmin,getting all data from a repository, grouped by category, but i can't figure out how to do so, whatever i'm trying to do
Upvotes: 1
Views: 56
Reputation: 63
Ok, so, for aynone who might be interested, the solution was quite easy, and i apologize for not having thought about it before, you just have to use an AssociationField with a group_by, this way :
$skillsField = AssociationField::new('skills', t('ui.mission.skills'))
->setFormTypeOption('group_by','category')
;
That's all folks, sorry for the disturbance, have a nice day !
Upvotes: 1