I have a Product Entity and a Category Entity which are linked together by a join table named "Categories_Products". Both entities contain a CollectionField and are built in ArrayCollection. For an example, here is the Product Entity with the Category field :
class Product
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
private $id;
#[ORM\ManyToMany(targetEntity: Category::class, mappedBy: "products")]
private Collection $categories;
public function __construct()
{
$this->categories = new ArrayCollection();
}
public function getCategories(): Collection
{
return $this->categories;
}
public function addCategory(Category $category): self
{
if (!$this->categories->contains($category)) {
$this->categories[] = $category;
$category->addProduct($this);
}
return $this;
}
public function removeCategory(Category $category): self
{
if (!$this->categories->contains($category)) {
$category->removeProduct($this);
}
return $this;
}
I would like to add the possibility to add few categories in the product entity, when adding a product. I use the EasyAdminBundle to display my forms.
Here is the field I added in the configureFields function:
public function configureFields(string $pageName): iterable
{
return [
TextField::new('name'),
CollectionField::new('categories')->setFormType(CategoriesType::class),
];
}
Knowing that I tried to create my own input form named "CategoriesType :
<?php
namespace App\Form;
use App\Entity\Category;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\OptionsResolver\OptionsResolver;
class CategoriesType extends AbstractType
{
private $entityManager;
public function __construct(EntityManagerInterface $entityManager)
{
$this->entityManager = $entityManager;
}
public function configureOptions(OptionsResolver $resolver)
{
$categories = $this->entityManager->getRepository(Category::class)->findAll();
$choices = [];
foreach ($categories as $category) {
$choices[$category->getName()] = $category->getId();
}
$resolver->setDefaults([
'choices' => $choices,
'multiple' => true,
'expanded' => true,
'by_reference' => false,
'allow_add' => true,
'allow_delete' => false,
'delete_empty' => false,
'entry_options' =>
['attr' => ['class' => 'category-box']],
]);
}
public function getParent()
{
return ChoiceType::class;
}
}
When I try to add a product, it returns this error : "Unable to transform value for property path "categories": Expected an array."
Can someone explain me how can I cope with EasyAdmin and manyToMany relation please ?
I tried to modify the Field in EasyAdmin : Instead of CollectionField, I tried only "Field". And it returned : "The Doctrine type of the "categories" field is "8", which is not supported by EasyAdmin."
and same error when I did this :
Field::new('categories', DoctrineEntityType::class)
->setFormTypeOptions([
'class' => Category::class,
'multiple' => true,
])
->setFormTypeOption('by_reference', false),