Symfony使用复选框自定义多对多

时间:2016-03-24 23:25:44

标签: php symfony symfony-forms

会话参与者,我正在尝试创建会话,并且包含所有人员的列表,以检查参与者

PersonInSession表格保留participants_idsession_id

我收到以下错误:

  

无法转换属性路径的值" person":预期Doctrine \ Common \ Collections \ Collection对象。

我想创建一个Session,并以相同的形式直接检查参与者。

PersonEntityType

class PersonEntityType extends AbstractType
{    
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('name');
    }
}

SessionEntityType

class SessionEntityType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('description')
            ->add('participant', CollectionType::class, 
                array(
                   'entry_type' => ParticipantEntityType::class,
                   'by_reference' => false,
                   'allow_add'    => true,
                   'allow_delete' => true,
                )
            );
    }
}

ParticipantEntityType

class ParticipantEntityType extends AbstractType
{   
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('person', EntityType::class,
                array(
                    'class' => 'AppBundle:PersonEntity',
                    'choice_label' => 'name',
                    'expanded' =>'true',
                    'multiple' => 'true'
                )
            );
    }
}

PersonEntity

class PersonEntity
{
    private $id;
    private $name;

    /**
     * @ORM\OneToMany(targetEntity="ParticipantEntity", mappedBy="person")
     */
    private $participant;

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

SessionEntity

class SessionEntity
{
    private $id;
    private $description;

    /**
     * @ORM\OneToMany(targetEntity="ParticipantEntity", mappedBy="session")
     */
    private $participant;

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

ParticipantEntity

class ParticipantEntity
{
    private $id;

    /**
     * @ORM\ManyToOne(targetEntity="PersonEntity", inversedBy="participant")
     */
    private $person;

    /**
    * @ORM\ManyToOne(targetEntity="SessionEntity", inversedBy="participant")
    */
    private $session;

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

2 个答案:

答案 0 :(得分:0)

你应该:

namespace AppBundle\Entity;

use Doctrine\Common\Collections\ArrayCollection

class ParticipantEntity
{
    // ...

    /**
     * @var PersonEntity[]|ArrayCollection
     */
    private $person;

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

    // ...

否则它无法正常工作。

答案 1 :(得分:0)

SessionEntityType中的

participant字段(您最好将其重命名为participants,因为那样更有意义)是CollectionType字段。因此,在entry_typeParticipantEntityType)中,您需要添加一个映射到目标实体(ParticipantEntity)的集合属性的字段。

在该实体中,您personManyToOne的关系错误participant。所以应该定义为:

<强> ParticipantEntity

class ParticipantEntity
{
    /**
     * @ORM\OneToMany(targetEntity="PersonEntity")
     */
    private $persons;

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

我强烈建议您修改您的实体,因为它们显然是问题的根源。