1
votes

I'm learning Symfony 2 and I got the following error when I submit a form:

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

Here is my code :

Controller :

    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(),
         ));
    }

My form type 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
        ))
    ;
}

My typeSponsor and Sponsor entities are linked by a ManyToMany relation.

Any idea what's wrong?

UPDATE :

This is my entity :

<?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
i think problem is somewhere in your entity, write more stacktrace and more of your entity.Marino Di Clemente
Yea, what does Sponsor::__construct() look like?Peter Bailey
I updated my post and add my entity contentAragornD7

1 Answers

7
votes

The problem is that your $typeSponsor property is a ManyToMany relation, but you use it as if it was a ManyToOne or OneToOne relation :

/**
  * @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;
}

You should it ManyToOne in the property annotation.

Edit :

If you want to use a ManyToMany relation, you have to change this in your entity :

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;
}

Because your field is a collection of TypeSponsor, not a single object.

Take a look at this article : http://www.keithwatanabe.net/2013/10/17/symfony-2-many-to-many-relationships-and-form-elements/