1
votes

I try to create OneToMany association in my Creation entity.

Creation

class Creation
{
    // ...

    /**
     * @ORM\OneToMany(targetEntity="App\Entity\Categorie", cascade={"persist"}, mappedBy="creation")
     */
    private $categories;

    // ...
}

CreationType

class CreationType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('title')
            ->add('description')
            ->add('categories', EntityType::class,  array(
                'class'         => 'App\Entity\Categorie',
                'choice_label'  => 'label',
                'multiple'      => true,
            ));

    }
}

Now, I want display my Categories in my twig template :

{% for categorie in creation.categories %}
    {{ categorie.label }}
{% endfor %}

I have this error, whereas I can display in dump this creation :

An exception has been thrown during the rendering of a template ("Notice: Undefined index: creation").

Someone can help ? Thanks

1
Thanks for help. In my form, when I use EntityType, I can see all my Categories and choose them. Not with Collection. ->add('categories', CollectionType::class, array( 'entry_type' => CategorieType::class, )) do I use it correctly? - Quentles

1 Answers

0
votes

EntityType inside your form is ok. you should just pass creation into your twig view, inside your controller.

You probably have a controller section like this:

class CreationController
{
    // ...

    public function showAction(Request $request)
    {
        $creation = $this->getDoctrine()->getManager()->getRepository(Creation::class)->find($request->get('id'));

        return $this->render('creation_show.html.twig', ['creation' => $creation]);
    }

    // ...
}

You should pass the $creation object as creation into your template so that you will be able to use it inside twig template