2
votes

My current setup is as described below. What i want to achive is. One Distributor can have multiple categories. But One category can have 1 Distributor 1:N <=> N:1. But it fails when i click create category even if the distributor input field is empty.

Category

/**
 * @var string
 *
 * @ORM\Id()
 * @ORM\Column(type="string", nullable=false, unique=true)
 * @ORM\GeneratedValue(strategy="UUID")
 */
private $id;

/**
 * @var string
 * @ORM\Column(type="string", nullable=false)
 */
private $title;

/**
 * @var Distributor
 * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Distributor", inversedBy="category")
 * @ORM\JoinColumn(referencedColumnName="id")
 */
private $distributor;

Distributor:

/**
 * @ORM\Column(type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

/**
 * @ORM\Column(type="string", length=100)
 */
private $name;

/**
 * @var ArrayCollection
 * @ORM\OneToMany(targetEntity="AppBundle\Entity\Category", mappedBy="distributor")
 */
private $category;

public function __construct()
{
    $this->category = new ArrayCollection();
}

CategoryForm:

$builder
        ->add('parent', EntityType::class, [
            'class' => Category::class,
            'choice_label' => 'title',
            'multiple' => true,
            'required' => false,
            'attr' => [
                'class' => 'select2 form-control select2insidemodal js-example-matcher'
            ]
        ])
        ->add('title', TextType::class, [
            'label' => 'Title',
            'required' => true,
            'by_reference' => true
        ])
        ->add('distributor', EntityType::class, [
            'class' => Distributor::class,
            'choice_label' => 'name',
            'required' => false,
            'attr' => [
                'class' => 'select2 form-control select2insidemodal js-example-matcher'
            ]
        ]);

Create Category Action

public function createAction(Request $request)
    {
        $category = new Category();
        $categoryForm = $this->createForm(CategoryForm::class, $category);
        $categoryForm->handleRequest($request);


        if ($categoryForm->isSubmitted() && $categoryForm->isValid()) {
            $result = $this->categoryService->create($category);

        }

        return $this->render(
            '@app_bar/Category/categoryNew.twig',
            [
                'form' => $categoryForm->createView(),
            ]
        );
}

The error message I get :

Expected argument of type "AppBundle\Entity\Category", "Doctrine\Common\Collections\ArrayCollection" given

1
what about your parent field? can you provide its annotation?Andrew Vakhniuk
if the parent is collection? or it is only one Category?Andrew Vakhniuk

1 Answers

2
votes

As i understood , parent is not a collection, so change the form parent multiple option to false:

 ->add('parent', EntityType::class, [
        'class' => Category::class,
        'choice_label' => 'title',
        'multiple' => false,
        'required' => false,
        'attr' => [
            'class' => 'select2 form-control select2insidemodal js-example-matcher'
        ]
    ])