jeudi 19 janvier 2017

Symfony2 Checkbox List

I have a Many to Many relation in my project but I want to modify my relation table, so I converted it into a two Many to One relation with the join table on another entity.

The problem is that when I try to render a checkbox group of the en two initial entities I'm not able to use then.

Here is my code:

class Professional extends User
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

...

/**
     * @ORM\OneToMany(targetEntity="TurnsProfessional", mappedBy="professional")
     */
    private $turns;

...

My second entity

class Turn
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;
...

  /**
     * @ORM\OneToMany(targetEntity="TurnsProfessional", mappedBy="turn")
     */
    private $professionals;

...

And the join entity

class TurnsProfessional
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    public function __construct()
    {
    }

    /**
     * @ORM\ManyToOne(targetEntity="Turn", inversedBy="professionals")
     * @ORM\JoinColumn(name="turn_id", referencedColumnName="id")
     */
    private $turn;

    /**
     * @ORM\ManyToOne(targetEntity="Professional", inversedBy="turns")
     * @ORM\JoinColumn(name="professional_id", referencedColumnName="id")
     */
    private $professional;

    /**
     * @ORM\Column(type="boolean")
     */
    private $status = 0;
 ...

My goal is to create a checkbox list in the Professional form with the turns. I've tried two things:

First I've tried to add in the ProfessionalType the field as a Turn::class:

...
 ->add('turns', 'entity',
            array('class' => 'AppBundle:TurnsProfessional',
                'property' => 'label',
                'multiple' => true,
                'expanded' => true,
                ));
...

The problem here is that the form does not render the checkboxes. Otherwise if I change the class:

...
->add('turns', 'entity',
            array('class' => 'AppBundle:Turn',
                'property' => 'label',
                'multiple' => true,
                'expanded' => true,
                ));
...

The form does render all the checkboxes but i get an error on sending it:

Found entity of type AppBundle\Entity\Turn on association AppBundle\Entity\Professional#turns, but expecting AppBundle\Entity\TurnsProfessional

Does anyone can help? Thank you!

Aucun commentaire:

Enregistrer un commentaire