title: Symfony2 Error: Attempted to call method on class "Doctrine\Common\Collections\ArrayCollection"
body: <p>I have a problem with my Symfony project. I ask question on Stack Overflow because everything seems good for me but it's apparently not... 
  In my project I have a OneToOne bidirectional Doctrine relation between two table named "Visiteur" and "Coordonnees". </p>
  
  <p>My problem appears when I submit my visiteur form. To be clear this form persist some data in "visiteur" table and it persist some data in "coordonnees" table ("imbricated form" translation from French to English)</p>
  
  <p>Then I have the error below :</p>
  
  <blockquote>
    <p>Attempted to call method "setVisiteur" on class
    "Doctrine\Common\Collections\ArrayCollection".</p>
  </blockquote>
  
  <p>There is my visiteurHandler.php who persist handle my data below.
  The error appears in line 54:  </p>
  
  <pre><code>$coordonnees-&gt;setVisiteur($visiteur);  
  </code></pre>
  
  <p>The line below help me to be sure of my data type :</p>
  
  <pre><code>var_dump(gettype($coordonnees));   
  </code></pre>
  
  <p>I obtain : string(6) "object" which is normal.</p>
  
  <p>
  
  <pre><code>namespace Was\RHBundle\Form;
  
  use Symfony\Component\Form\Form;
  use Symfony\Component\HttpFoundation\Request;
  use Doctrine\ORM\EntityManager;
  use Was\RHBundle\Entity\Visiteur;
  use Was\RHBundle\Entity\Coordonnees;
  
  use Symfony\Component\DependencyInjection\ContainerInterface;
  use Symfony\Component\HttpFoundation\Response;
  
  class VisiteurHandler
  {
  protected $form;
  protected $request;
  protected $em;
  
  public function __construct(Form $form, Request $request, EntityManager $em)
  {
      $this-&gt;form = $form;
      $this-&gt;request = $request;
      $this-&gt;em = $em;
  }
  
  public function process()
  {
  
      if ($this-&gt;request-&gt;getMethod() == 'POST') {
          $this-&gt;form-&gt;bind($this-&gt;request);
  
          if ($this-&gt;form-&gt;isValid() ) {
          // echo '&lt;pre&gt;';
          // print_r($this-&gt;request);
          // echo '&lt;/pre&gt;';
          // die();
          $this-&gt;onSuccess($this-&gt;form-&gt;getData());
  
          return true;
          }
      }
  
  return false;
  }
  
  public function onSuccess(Visiteur $visiteur)
  {
      $coordonnees=$visiteur-&gt;getCoordonnees();
      $adresse=$visiteur-&gt;getAdresse();
  
  
      var_dump(gettype($coordonnees));        
      $coordonnees-&gt;setVisiteur($visiteur);
      $adresse-&gt;setVisiteur($visiteur);
  
  
      $this-&gt;em-&gt;persist($coordonnees);
      $this-&gt;em-&gt;persist($adresse);
  
      $visiteur-&gt;setCoordonnees($coordonnees);
      $visiteur-&gt;setAdresse($adresse);
  
      $this-&gt;em-&gt;persist($visiteur);
      $this-&gt;em-&gt;flush();
  }
  
  }
  </code></pre>
  
  <p>This is my entity visiteur which is my main entity : </p>
  
  <pre><code>&lt;?php
  
  namespace Was\RHBundle\Entity;
  
  use Doctrine\ORM\Mapping as ORM;
  use Symfony\Component\Validator\Constraints as Assert;
  use Symfony\Component\Validator\Context\ExecutionContext;
  use Doctrine\Common\Collections\ArrayCollection;
  
  /**
   * Was\RHBundle\Entity\Visiteur
   *
   * @ORM\Table()
   * @ORM\Entity(repositoryClass="Was\RHBundle\Entity\VisiteurRepository")
   * 
   */
  class Visiteur
  {
  
  
  public function __toString()
  {
  return ucwords($this-&gt;prenom . " " . $this-&gt;nom);
  }
  
  public function __construct()
  {
      $this-&gt;createdAt = new \DateTime();
      $this-&gt;vehicules = new \Doctrine\Common\Collections\ArrayCollection();
      $this-&gt;coordonnees = new \Doctrine\Common\Collections\ArrayCollection();
  
  }
  
  
  /**
   * @var integer $id
   *
   * @ORM\Column(name="id", type="integer")
   * @ORM\Id
   * @ORM\GeneratedValue(strategy="AUTO")
   */
  private $id;
  
  /**
   * @var datetime $createdAt
   *
   * @ORM\Column(name="createdAt", type="datetime")
   */
  private $createdAt;
  
  
  /**
   * @var string $nom
   *
   * @ORM\Column(name="nom", type="string", length=255)
   */
  private $nom;
  
  /**
   * @var string $prenom
   *
   * @ORM\Column(name="prenom", type="string", length=255)
   */
  private $prenom;
  
  /**
   * @var date $dateDebut
   *
   * @ORM\Column(name="dateDebut", type="date")
   */
  private $dateDebut;
  
  /**
   * @var date $dateFin
   *
   * @ORM\Column(name="dateFin", type="date")
   */
  private $dateFin;
  
  /**
   * @var string $service
   *
   * @ORM\Column(name="service", type="string", length=255)
   */
  private $service;
  
  /**
   * @var string $fonction
   *
   * @ORM\Column(name="fonction", type="string", length=255)
   */
  private $fonction;
  
  
  /**
   * @var text $remarqueSecurite
   *
   * @ORM\Column(name="remarqueSecurite", type="text", nullable=true)
   */
  private $remarqueSecurite;
  
  /**
   * @ORM\OneToMany(targetEntity="Vehicule", mappedBy="visiteur", cascade={"remove"})
   */
  private $vehicules;
  
  /**
   * @ORM\OneToOne(targetEntity="Was\RHBundle\Entity\Coordonnees", mappedBy="visiteur", cascade={"remove"})
   */
  private $coordonnees;
  
  /**
   * @ORM\OneToOne(targetEntity="Adresse", mappedBy="visiteur", cascade={"remove"})
   */
  private $adresse;
  
  /**
   * @ORM\ManyToOne(targetEntity="Agent")
   * @ORM\JoinColumn(name="agent_id", referencedColumnName="id", nullable=true)
   */
  private $hote;
  
  
  
  
  public function isEnCours()
  {
      $maintenant = new \DateTime();
      if ($maintenant &gt; $this-&gt;dateDebut &amp;&amp; $maintenant &lt;= $this-&gt;dateFin || $this-&gt;dateFin == null) return true;
      return false;
  }
  
  public function isAncien()
  {
      $maintenant = new \DateTime();
      if ($maintenant &gt; $this-&gt;dateDebut &amp;&amp; $maintenant &gt; $this-&gt;dateFin) return true;
      return false;
  }
  
  public function isFutur()
  {
      $maintenant = new \DateTime();
      if ($maintenant &lt; $this-&gt;dateDebut &amp;&amp; $maintenant &lt;= $this-&gt;dateFin || $this-&gt;dateFin == null) return true;
      return false;
  }
  
  
  /**
   * Get id
   *
   * @return integer 
   */
  public function getId()
  {
      return $this-&gt;id;
  }
  
  /**
   * Set nom
   *
   * @param string $nom
   */
  public function setNom($nom)
  {
      $this-&gt;nom = $nom;
  }
  
  /**
   * Get nom
   *
   * @return string 
   */
  public function getNom()
  {
      return $this-&gt;nom;
  }
  
  /**
   * Set prenom
   *
   * @param string $prenom
   */
  public function setPrenom($prenom)
  {
      $this-&gt;prenom = $prenom;
  }
  
  /**
   * Get prenom
   *
   * @return string 
   */
  public function getPrenom()
  {
      return $this-&gt;prenom;
  }
  
  /**
   * Set dateDebut
   *
   * @param date $dateDebut
   */
  public function setDateDebut($dateDebut)
  {
      $this-&gt;dateDebut = $dateDebut;
  }
  
  /**
   * Get dateDebut
   *
   * @return date 
   */
  public function getDateDebut()
  {
      return $this-&gt;dateDebut;
  }
  
  /**
   * Set dateFin
   *
   * @param date $dateFin
   */
  public function setDateFin($dateFin)
  {
      $this-&gt;dateFin = $dateFin;
  }
  
  /**
   * Get dateFin
   *
   * @return date 
   */
  public function getDateFin()
  {
      return $this-&gt;dateFin;
  }
  
  /**
   * Set service
   *
   * @param string $service
   */
  public function setService($service)
  {
      $this-&gt;service = $service;
  }
  
  /**
   * Get service
   *
   * @return string 
   */
  public function getService()
  {
      return $this-&gt;service;
  }
  
  /**
   * Set remarqueSecurite
   *
   * @param text $remarqueSecurite
   */
  public function setRemarqueSecurite($remarqueSecurite)
  {
      $this-&gt;remarqueSecurite = $remarqueSecurite;
  }
  
  /**
   * Get remarqueSecurite
   *
   * @return text 
   */
  public function getRemarqueSecurite()
  {
      return $this-&gt;remarqueSecurite;
  }
  
  
  /**
   * Add vehicules
   *
   * @param Was\RHBundle\Entity\Vehicule $vehicules
   */
  public function addVehicule(\Was\RHBundle\Entity\Vehicule $vehicules)
  {
      $this-&gt;vehicules[] = $vehicules;
  }
  
  /**
   * Get vehicules
   *
   * @return Doctrine\Common\Collections\Collection 
   */
  public function getVehicules()
  {
      return $this-&gt;vehicules;
  }
  
  /**
   * Set coordonnees
   *
   * @param Was\RHBundle\Entity\Coordonnees $coordonnees
   */
  public function setCoordonnees(Coordonnees $coordonnees)
  {
      $this-&gt;coordonnees[] = $coordonnees;
      //$coordonnees-&gt;setVisiteur($this);
      //return $this;
  
  }
  
  /**
   * Get coordonnees
   *
   * @return Was\RHBundle\Entity\Coordonnees 
   */
  public function getCoordonnees()
  {
      return $this-&gt;coordonnees;
  }
  
  /**
   * Set adresse
   *
   * @param Was\RHBundle\Entity\Adresse $adresse
   */
  public function setAdresse(\Was\RHBundle\Entity\Adresse $adresse)
  {
      $this-&gt;adresse = $adresse;
  }
  
  /**
   * Get adresse
   *
   * @return Was\RHBundle\Entity\Adresse 
   */
  public function getAdresse()
  {
      return $this-&gt;adresse;
  }
  
  /**
   * Set createdAt
   *
   * @param datetime $createdAt
   */
  public function setCreatedAt($createdAt)
  {
      $this-&gt;createdAt = $createdAt;
  }
  
  /**
   * Get createdAt
   *
   * @return datetime 
   */
  public function getCreatedAt()
  {
      return $this-&gt;createdAt;
  }
  
  /**
   * Set fonction
   *
   * @param string $fonction
   */
  public function setFonction($fonction)
  {
      $this-&gt;fonction = $fonction;
  }
  
  /**
   * Get fonction
   *
   * @return string 
   */
  public function getFonction()
  {
      return $this-&gt;fonction;
  }
  
  /**
   * Set hote
   *
   * @param Was\RHBundle\Entity\Agent $hote
   */
  public function setHote(\Was\RHBundle\Entity\Agent $hote)
  {
      $this-&gt;hote = $hote;
  }
  
  /**
   * Get hote
   *
   * @return Was\RHBundle\Entity\Agent 
   */
  public function getHote()
  {
      return $this-&gt;hote;
  }
  
  /**
   * Remove vehicules
   *
   * @param Was\RHBundle\Entity\Vehicule $vehicules
   */
  public function removeVehicule(\Was\RHBundle\Entity\Vehicule $vehicules)
  {
      $this-&gt;vehicules-&gt;removeElement($vehicules);
  }
  }
  </code></pre>
  
  <p>This my coordonnees Entity :
      
  
  <pre><code> namespace Was\RHBundle\Entity;
  
  use Doctrine\ORM\Mapping as ORM;
  
  use Symfony\Component\Validator\Constraints as Assert;
  
  use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  
  use Was\UserBundle\Entity\User as User;
  
  
  /**
   * Was\RHBundle\Entity\Coordonnees
   *
   * @ORM\Table()
   * @ORM\Entity(repositoryClass="Was\RHBundle\Entity\CoordonneesRepository")
   * @UniqueEntity(fields="emailPro", message="Cet email professionnel est     déjà pris.")
   */
  class Coordonnees
  {
  /**
   * @var integer $id
   *
   * @ORM\Column(name="id", type="integer")
   * @ORM\Id
   * @ORM\GeneratedValue(strategy="AUTO")
   */
  private $id;
  
   /**
    * @var string $telPro
     *
    * @ORM\Column(name="telPro", type="string", length=255, nullable=true)
    */
  private $telPro;
  
    /**
     * @var string $telFax
     *
     * @ORM\Column(name="telFax", type="string", length=255, nullable=true)
     */
  private $telFax;
  
  /**
   * @var string $telPortable
   *
   * @ORM\Column(name="telPortable", type="string", length=255, nullable=true)
   */
  private $telPortable;
  
  /**
   * @var string $telDomicile
   *
   * @ORM\Column(name="telDomicile", type="string", length=255, nullable=true)
   */
  private $telDomicile;
  
  /**
   * @var string $telAutre
   *
   * @ORM\Column(name="telAutre", type="string", length=255, nullable=true)
   */
  private $telAutre;
  
  /**
   * @var string $telUrgence
   *
   * @ORM\Column(name="telUrgence", type="string", length=255, nullable=true)
   */
  private $telUrgence;
  
  
  /**
   * @ORM\Column(name="contactUrgence", type="text", nullable=true)
   */
  private $contactUrgence;
  
  /**
   * @ORM\Column(name="contactUrgenceUS", type="text", nullable=true)
   */
  private $contactUrgenceUS;
  
  /**
   * @var string $numeroBadge
   *
   * @ORM\Column(name="numeroBadge", type="string", length=255, nullable=true)
   */
  private $numeroBadge;
  
  
  /**
   * @ORM\Column(type="string", length=255, nullable=true)
   * @Assert\Email(message="Email personnel invalide.")
   */
  private $emailPerso;
  
  /**
   * @ORM\Column(type="string", length=255, nullable=true)
   * @Assert\Email(message="Email professionnel invalide.")
   */
  private $emailPro;
  
  
  
  /**
   * @ORM\OneToOne(targetEntity="Was\RHBundle\Entity\Agent", inversedBy="coordonnees")
   * @ORM\JoinColumn( name="agent_id", referencedColumnName="id")
   */
  private $agent;
  
  /**
   * @ORM\OneToOne(targetEntity="Was\RHBundle\Entity\Visiteur", inversedBy="coordonnees")
   * @ORM\JoinColumn(name="visiteur_id", referencedColumnName="id")
   */
  private $visiteur;
  
  
  /**
    * @var datetime $updatedAt
    *
    * @ORM\Column(name="updatedAt", type="datetime", nullable=true)
    */
   private $updatedAt;
  
  
  /**
   * @ORM\ManyToOne(targetEntity="\Was\UserBundle\Entity\User")
   * @ORM\JoinColumn(name="updated_by_id", referencedColumnName="id", nullable=true)
   */
  private $updatedBy;
  
  
  
  /**
   * Get id
   *
   * @return integer 
   */
  public function getId()
  {
      return $this-&gt;id;
  }
  
  /**
   * Set telPro
   *
   * @param string $telPro
   */
  public function setTelPro($telPro)
  {
      $this-&gt;telPro = $telPro;
  }
  
  /**
   * Get telPro
   *
   * @return string 
   */
  public function getTelPro()
  {
      return $this-&gt;telPro;
  }
  
  /**
   * Set telFax
   *
   * @param string $telFax
   */
  public function setTelFax($telFax)
  {
      $this-&gt;telFax = $telFax;
  }
  
  /**
   * Get telFax
   *
   * @return string 
   */
  public function getTelFax()
  {
      return $this-&gt;telFax;
  }
  
  /**
   * Set telPortable
   *
   * @param string $telPortable
   */
  public function setTelPortable($telPortable)
  {
      $this-&gt;telPortable = $telPortable;
  }
  
  /**
   * Get telPortable
   *
   * @return string 
   */
  public function getTelPortable()
  {
      return $this-&gt;telPortable;
  }
  
  /**
   * Set telDomicile
   *
   * @param string $telDomicile
   */
  public function setTelDomicile($telDomicile)
  {
      $this-&gt;telDomicile = $telDomicile;
  }
  
  /**
   * Get telDomicile
   *
   * @return string 
   */
  public function getTelDomicile()
  {
      return $this-&gt;telDomicile;
  }
  
  /**
   * Set telAutre
   *
   * @param string $telAutre
   */
  public function setTelAutre($telAutre)
  {
      $this-&gt;telAutre = $telAutre;
  }
  
  /**
   * Get telAutre
   *
   * @return string 
   */
  public function getTelAutre()
  {
      return $this-&gt;telAutre;
  }
  
  /**
   * Set telUrgence
   *
   * @param string $telUrgence
   */
  public function setTelUrgence($telUrgence)
  {
      $this-&gt;telUrgence = $telUrgence;
  }
  
  /**
   * Get telUrgence
   *
   * @return string 
   */
  public function getTelUrgence()
  {
      return $this-&gt;telUrgence;
  }
  
  /**
   * Set agent
   *
   * @param Was\RHBundle\Entity\Agent $agent
   */
  public function setAgent(\Was\RHBundle\Entity\Agent $agent)
  {
      $this-&gt;agent = $agent;
  }
  
  /**
   * Get agent
   *
   * @return Was\RHBundle\Entity\Agent 
   */
  public function getAgent()
  {
      return $this-&gt;agent;
  }
  
  /**
   * Set emailPerso
   *
   * @param string $emailPerso
   */
  public function setEmailPerso($emailPerso)
  {
      $this-&gt;emailPerso = $emailPerso;
  }
  
  /**
   * Get emailPerso
   *
   * @return string 
   */
  public function getEmailPerso()
  {
      return $this-&gt;emailPerso;
  }
  
  /**
   * Set emailPro
   *
   * @param string $emailPro
   */
  public function setEmailPro($emailPro)
  {
      $this-&gt;emailPro = $emailPro;
  }
  
  /**
   * Get emailPro
   *
   * @return string 
   */
  public function getEmailPro()
  {
      return $this-&gt;emailPro;
  }
  
  /**
   * Set visiteur
   *
   * @param Was\RHBundle\Entity\Visiteur $visiteur
   */
  public function setVisiteur(\Was\RHBundle\Entity\Visiteur $visiteur)
  {
      $this-&gt;visiteur = $visiteur;
  }
  
  /**
   * Get visiteur
   *
   * @return Was\RHBundle\Entity\Visiteur 
   */
  public function getVisiteur()
  {
      return $this-&gt;visiteur;
  }
  
  /**
   * Set updatedAt
   *
   * @param datetime $updatedAt
   */
  public function setUpdatedAt($updatedAt)
  {
      $this-&gt;updatedAt = $updatedAt;
  }
  
  /**
   * Get updatedAt
   *
   * @return datetime 
   */
  public function getUpdatedAt()
  {
      return $this-&gt;updatedAt;
  }
  
  /**
   * Set updatedBy
   *
   * @param Was\UserBundle\Entity\User $updatedBy
   */
  public function setUpdatedBy($updatedBy)
  {
      $this-&gt;updatedBy = $updatedBy;
  }
  
  /**
   * Get updatedBy
   *
   * @return Was\UserBundle\Entity\User 
   */
  public function getUpdatedBy()
  {
      return $this-&gt;updatedBy;
  }
  
  /**
   * Set numeroBadge
   *
   * @param string $numeroBadge
   */
  public function setNumeroBadge($numeroBadge)
  {
      $this-&gt;numeroBadge = $numeroBadge;
  }
  
  /**
   * Get numeroBadge
   *
   * @return string 
   */
  public function getNumeroBadge()
  {
      return $this-&gt;numeroBadge;
  }
  
  /**
   * Set contactUrgence
   *
   * @param text $contactUrgence
   */
  public function setContactUrgence($contactUrgence)
  {
      $this-&gt;contactUrgence = $contactUrgence;
  }
  
  /**
   * Get contactUrgence
   *
   * @return text 
   */
  public function getContactUrgence()
  {
      return $this-&gt;contactUrgence;
  }
  
  /**
   * Set contactUrgenceUS
   *
   * @param text $contactUrgenceUS
   */
  public function setContactUrgenceUS($contactUrgenceUS)
  {
      $this-&gt;contactUrgenceUS = $contactUrgenceUS;
  }
  
  /**
   * Get contactUrgenceUS
   *
   * @return text 
   */
  public function getContactUrgenceUS()
  {
      return $this-&gt;contactUrgenceUS;
  }
  }
  </code></pre>