2015-05-19 43 views
1

我在學習Symfony 2,並且在提交表單時出現以下錯誤:可捕獲的致命錯誤:傳遞給Doctrine Common Collections ArrayCollection :: __ construct()的參數1必須是類型數組,對象給定

Catchable Fatal Error: Argument 1 passed to Doctrine\Common\Collections\ArrayCollection::__construct() must be of the type array, object given, called in C:\xampp\htdocs\ppe2\vendor\doctrine\orm\lib\Doctrine\ORM\UnitOfWork.php on line 555 and defined

這裏是我的代碼:

控制器:

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關係鏈接。

任何想法有什麼不對?

UPDATE:

這是我的實體:

<?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; 
    } 
} 
+0

我認爲問題是在實體的地方,寫更多的堆棧跟蹤和更多的實體。 –

+0

是的,'Sponsor :: __ construct()'看起來像什麼? –

+0

我更新了我的文章並添加了我的實體內容 – AragornD7

回答

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

編輯:

如果你想使用一個多對多關係,你有你的實體可以改變:

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/

+0

感謝您的回答,但在我的代碼中,我需要更改什麼?因爲我想要ManyToMany關係,並且我的來自var $ typeSponsor的註釋已經在ManyToMany中。 – AragornD7

+0

請注意,我需要明天展示我的項目,這是我的最後一個問題:/ – AragornD7

+0

ManyToMany關係意味着您的贊助商可以有多個sponsorType,反之亦然。根據你的表單聲明,你只允許爲你的贊助商設置一個sponsorType,所以它不會與ManyToMany一起工作。所以你有一個問題,在你的形式或實體中:贊助商是否可以擁有多個贊助者類型? –

相關問題