2

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

2
  • what about your parent field? can you provide its annotation? Commented May 25, 2018 at 0:50
  • if the parent is collection? or it is only one Category? Commented May 25, 2018 at 0:51

1 Answer 1

2

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'
        ]
    ])
Sign up to request clarification or add additional context in comments.

3 Comments

yes, i found it my self also. But thanks for the answer. But there is another issue which i am not able to resolve yet! All the categories which are added are added as top category. But if i edit an category i should be able to select multiple categories and place them as child under my parent category. How can i manage this ?
add field children OneToMany
it is/was already OneToMany i created an work around to get all the values of the childeren and put and parent value through the CategoryService. If you have an better solution please assist.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.