symfony中的manyToOne错误并形成叠加

时间:2016-11-15 11:27:05

标签: symfony

我在symfony3下遇到了一个问题,它阻止了我一段时间......我不明白这个错误:

Expected value of type "Doctrine\Common\Collections\Collection|array" for association field "AppBundle\Entity\Project#$participants", got "string" instead.

这是我的实体项目:

class Project
{
/**
 * @var integer
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

/**
 * @ORM\ManyToOne(targetEntity="AppBundle\Entity\User", inversedBy="projects")
 * @ORM\JoinColumn(nullable=false)
 */
private $user;

/**
 * @ORM\OneToMany(targetEntity="AppBundle\Entity\Participant", mappedBy="project", cascade={"persist", "remove"})
 */
private $participants;

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

/**
 * @var string
 *
 * @ORM\Column(name="content", type="text", nullable=true)
 */
private $content;

/**
 * @var string
 *
 * @ORM\Column(name="place", type="string", length=255, nullable=true)
 */
private $place;

/**
 * @var \DateTime
 *
 * @ORM\Column(name="dateEvent", type="datetime", nullable=true)
 */
private $dateEvent;

/**
 * @var boolean
 *
 * @ORM\Column(name="status", type="boolean")
 */
private $status;

/**
 * @var boolean
 *
 * @ORM\Column(name="in_progress", type="boolean")
 */
private $inProgress;

/**
 * @var boolean
 *
 * @ORM\Column(name="accept_list", type="boolean")
 */
private $acceptList;

/**
 * @var boolean
 *
 * @ORM\Column(name="visible_list", type="boolean")
 */
private $visibleList;

/**
 * @var boolean
 *
 * @ORM\Column(name="many_loop", type="boolean")
 */
private $manyLoop;

/**
 * @var string
 *
 * @ORM\Column(name="text_email", type="text", nullable=true)
 */
private $textEmail;

/**
 * @var \DateTime
 *
 * @ORM\Column(name="created", type="datetime")
 */
private $created;


public function __construct()
{
    $this->run          = false;
    $this->status       = false;
    $this->acceptList   = true;
    $this->visibleList  = true;
    $this->inProgress   = false;
    $this->manyLoop     = true;
    $this->created      = new \Datetime('now');
    $this->participants = new ArrayCollection();
}

/**
 * Get id
 *
 * @return integer
 */
public function getId()
{
    return $this->id;
}

/**
 * Set title
 *
 * @param string $title
 *
 * @return Project
 */
public function setTitle($title)
{
    $this->title = $title;

    return $this;
}

/**
 * Get title
 *
 * @return string
 */
public function getTitle()
{
    return $this->title;
}

/**
 * Set content
 *
 * @param string $content
 *
 * @return Project
 */
public function setContent($content)
{
    $this->content = $content;

    return $this;
}

/**
 * Get content
 *
 * @return string
 */
public function getContent()
{
    return $this->content;
}

/**
 * Set place
 *
 * @param string $place
 *
 * @return Project
 */
public function setPlace($place)
{
    $this->place = $place;

    return $this;
}

/**
 * Get place
 *
 * @return string
 */
public function getPlace()
{
    return $this->place;
}

/**
 * Set dateEvent
 *
 * @param \DateTime $dateEvent
 *
 * @return Project
 */
public function setDateEvent($dateEvent)
{
    $this->dateEvent = $dateEvent;

    return $this;
}

/**
 * Get dateEvent
 *
 * @return \DateTime
 */
public function getDateEvent()
{
    return $this->dateEvent;
}

/**
 * Set status
 *
 * @param boolean $status
 *
 * @return Project
 */
public function setStatus($status)
{
    $this->status = $status;

    return $this;
}

/**
 * Get status
 *
 * @return boolean
 */
public function getStatus()
{
    return $this->status;
}

/**
 * Set inProgress
 *
 * @param boolean $inProgress
 *
 * @return Project
 */
public function setInProgress($inProgress)
{
    $this->inProgress = $inProgress;

    return $this;
}

/**
 * Get inProgress
 *
 * @return boolean
 */
public function getInProgress()
{
    return $this->inProgress;
}

/**
 * Set acceptList
 *
 * @param boolean $acceptList
 *
 * @return Project
 */
public function setAcceptList($acceptList)
{
    $this->acceptList = $acceptList;

    return $this;
}

/**
 * Get acceptList
 *
 * @return boolean
 */
public function getAcceptList()
{
    return $this->acceptList;
}

/**
 * Set visibleList
 *
 * @param boolean $visibleList
 *
 * @return Project
 */
public function setVisibleList($visibleList)
{
    $this->visibleList = $visibleList;

    return $this;
}

/**
 * Get visibleList
 *
 * @return boolean
 */
public function getVisibleList()
{
    return $this->visibleList;
}

/**
 * Set manyLoop
 *
 * @param boolean $manyLoop
 *
 * @return Project
 */
public function setManyLoop($manyLoop)
{
    $this->manyLoop = $manyLoop;

    return $this;
}

/**
 * Get manyLoop
 *
 * @return boolean
 */
public function getManyLoop()
{
    return $this->manyLoop;
}

/**
 * Set textEmail
 *
 * @param string $textEmail
 *
 * @return Project
 */
public function setTextEmail($textEmail)
{
    $this->textEmail = $textEmail;

    return $this;
}

/**
 * Get textEmail
 *
 * @return string
 */
public function getTextEmail()
{
    return $this->textEmail;
}

/**
 * Set created
 *
 * @param \DateTime $created
 *
 * @return Project
 */
public function setCreated($created)
{
    $this->created = $created;

    return $this;
}

/**
 * Get created
 *
 * @return \DateTime
 */
public function getCreated()
{
    return $this->created;
}

/**
 * Set user
 *
 * @param \AppBundle\Entity\User $user
 *
 * @return Project
 */
public function setUser(\AppBundle\Entity\User $user)
{
    $this->user = $user;

    return $this;
}

/**
 * Get user
 *
 * @return \AppBundle\Entity\User
 */
public function getUser()
{
    return $this->user;
}

/**
 * Add participant
 *
 * @param \AppBundle\Entity\Participant $participant
 *
 * @return Project
 */
public function addParticipant(\AppBundle\Entity\Participant $participant)
{
    $this->participants[] = $participant;

    // On lie l'annonce à la candidature
    $participant->setProject($this);

    return $this;
}

/**
 * Remove participant
 *
 * @param \AppBundle\Entity\Participant $participant
 */
public function removeParticipant(\AppBundle\Entity\Participant $participant)
{
    $this->participants->removeElement($participant);
}

/**
 * Get participants
 *
 * @return \Doctrine\Common\Collections\Collection
 */
public function getParticipants()
{
    return $this->participants;
}}

我的表格:

$builder ->add('participants', ParticipantType::class);

/**
* {@inheritdoc}
*/
public function configureOptions (OptionsResolver $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => 'AppBundle\Entity\Project',
        'step' => 1,
    ));
}

形式叠加:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('lastName', TextType::class, array(
            'required'  => true,
            'label'     => 'Nom'
        ))
        ->add('firstName', TextType::class, array(
            'required'  => true,
            'label'     => 'Prénom'
        ))
        ->add('email', EmailType::class, array(
            'required'  => true,
            'label'     => 'Adresse mail'
        ));
}

/**
 * {@inheritdoc}
 */
public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults(array(
        'validation_groups' => array('registration')
    ));
}

所以这里总结一下我的问题,我不明白Symfony返回的错误,但是从互联网上的例子我不明白为什么这里有问题......

如果有人提出解决方案,那么我会在很长一段时间内阻止这个问题...

谢谢!

2 个答案:

答案 0 :(得分:0)

请确保您不要忘记添加:use Doctrine\Common\Collections\ArrayCollection或使用$this->participants = new \Doctrine\Common\Collections\ArrayCollection();

答案 1 :(得分:0)

FormType中,您应将participants字段配置为Participant的集合。应该使用CollectionType这样完成:

$builder->add('participants', CollectionType::class, array(
  'entry_type' => ParticipantType::class,
  'allow_add' => true,
  ...
));

修改 根据您的数据模型,participants字段是Participant对象的集合。正如您在错误消息中看到的那样,需要一个数组。 CollectionType将始终返回目标类型的数组。 这就是为什么我建议您考虑在表单类型中使用CollectionType。您可以了解有关CollectionType here以及Symfony表格的更多信息here