可捕获致命错误:传递给Doctrine \ Common \ Collections \ ArrayCollection :: __ construct()的参数1必须是类型数组,给定对象

时间:2015-05-19 15:32:28

标签: symfony

我正在学习Symfony 2,当我提交表单时出现以下错误:

  

捕获致命错误:传递给Doctrine \ Common \ Collections \ ArrayCollection :: __ construct()的参数1必须是类型数组,给定对象,在C:\ xampp \ htdocs \ ppe2 \ vendor \ doctrine \ orm \中调用第555行的lib \ Doctrine \ ORM \ UnitOfWork.php并定义了

这是我的代码:

控制器:

    public function ajouterAction(Request $request){

         $sponsor = new Sponsor();
         $formulaire = $this->createForm(new SponsorType(), $sponsor)->add('Ajouter', 'submit');

         if ($formulaire->handleRequest($request)->isValid()) {
           $em = $this->getDoctrine()->getManager();
           $em->persist($sponsor);
           $em->flush();

           $request->getSession()->getFlashBag()->add('notice', 'Le sponsor a bien été modfié !');

           return $this->redirect($this->generateUrl('ffe_sponsor_voir', array('id' => $sponsor->getId())));
         }

         return $this->render('FFESponsorBundle:Sponsor:ajouter.html.twig', array(
           'formulaire' => $formulaire->createView(),
         ));
    }

我的表单类型SponsorType:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('nom', 'text')
        ->add('rue', 'text')
        ->add('codePostal', 'text')
        ->add('ville', 'text')
        ->add('telephone', 'text')
        ->add('mail', 'text')
        ->add('nomRepresentant', 'text')
        ->add('typeSponsor', 'entity', array(
                'class'       => 'FFESponsorBundle:TypeSponsor',
                'property'    => 'nom',
                'multiple'    => true
        ))
    ;
}

我的typeSponsor和Sponsor实体通过ManyToMany关系链接。

知道什么是错的吗?

更新:

这是我的实体:

<?php

namespace FFE\SponsorBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Sponsor
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="FFE\SponsorBundle\Entity\SponsorRepository")
 */
class Sponsor
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

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

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

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

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

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

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

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

    /**
      * @ORM\ManyToMany(targetEntity="FFE\SponsorBundle\Entity\TypeSponsor", cascade={"persist"})
      */
    private $typeSponsor;

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

    /**
     * Set nom
     *
     * @param string $nom
     * @return Sponsor
     */
    public function setNom($nom)
    {
        $this->nom = $nom;

        return $this;
    }

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

    /**
     * Set rue
     *
     * @param string $rue
     * @return Sponsor
     */
    public function setRue($rue)
    {
        $this->rue = $rue;

        return $this;
    }

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

    /**
     * Set codePostal
     *
     * @param string $codePostal
     * @return Sponsor
     */
    public function setCodePostal($codePostal)
    {
        $this->codePostal = $codePostal;

        return $this;
    }

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

    /**
     * Set ville
     *
     * @param string $ville
     * @return Sponsor
     */
    public function setVille($ville)
    {
        $this->ville = $ville;

        return $this;
    }

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

    /**
     * Set telephone
     *
     * @param string $telephone
     * @return Sponsor
     */
    public function setTelephone($telephone)
    {
        $this->telephone = $telephone;

        return $this;
    }

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

    /**
     * Set mail
     *
     * @param string $mail
     * @return Sponsor
     */
    public function setMail($mail)
    {
        $this->mail = $mail;

        return $this;
    }

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

    /**
     * Set nomRepresentant
     *
     * @param string $nomRepresentant
     * @return Sponsor
     */
    public function setNomRepresentant($nomRepresentant)
    {
        $this->nomRepresentant = $nomRepresentant;

        return $this;
    }

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

    /**
     * Set typeSponsor
     *
     * @param \FFE\SponsorBundle\Entity\TypeSponsor $typeSponsor
     * @return Sponsor
     */
    public function setTypeSponsor(\FFE\SponsorBundle\Entity\TypeSponsor $typeSponsor = null)
    {
        $this->typeSponsor = $typeSponsor;

        return $this;
    }

    /**
     * Get typeSponsor
     *
     * @return \FFE\SponsorBundle\Entity\TypeSponsor 
     */
    public function getTypeSponsor()
    {
        return $this->typeSponsor;
    }
}

1 个答案:

答案 0 :(得分:7)

问题是您的$ typeSponsor属性是ManyToMany关系,但您可以将其用作ManyToOneOneToOne关系:

/**
  * @ORM\ManyToMany(targetEntity="FFE\SponsorBundle\Entity\TypeSponsor", cascade={"persist"})
  */
private $typeSponsor;

/**
 * Set typeSponsor
 *
 * @param \FFE\SponsorBundle\Entity\TypeSponsor $typeSponsor
 * @return Sponsor
 */
public function setTypeSponsor(\FFE\SponsorBundle\Entity\TypeSponsor $typeSponsor = null)
{
    $this->typeSponsor = $typeSponsor;

    return $this;
}

/**
 * Get typeSponsor
 *
 * @return \FFE\SponsorBundle\Entity\TypeSponsor 
 */
public function getTypeSponsor()
{
    return $this->typeSponsor;
}

你应该在属性注释中ManyToOne

编辑:

如果要使用ManyToMany关系,则必须在实体中更改此关系:

public function __construct()
{
   $this->sponsorType = new \Doctrine\Common\Collection\ArrayCollection();
}

/**
 * Set typeSponsor
 *
 * @param \Doctrine\Common\Collection\ArrayCollection $typeSponsor
 * @return Sponsor
 */
public function setTypeSponsor($typeSponsor)
{
    $this->typeSponsor = $typeSponsor;

    return $this;
}

/**
 * Get typeSponsor
 *
 * @return \Doctrine\Common\Collection\ArrayCollection
 */
public function getTypeSponsor()
{
    return $this->typeSponsor;
}

因为您的字段是TypeSponsor的集合,而不是单个对象。

看一下这篇文章:http://www.keithwatanabe.net/2013/10/17/symfony-2-many-to-many-relationships-and-form-elements/