简体   繁体   中英

Symfony2 Error: Attempted to call method on class “Doctrine\Common\Collections\ArrayCollection”

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".

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)

Then I have the error below :

Attempted to call method "setVisiteur" on class "Doctrine\\Common\\Collections\\ArrayCollection".

There is my visiteurHandler.php who persist handle my data below. The error appears in line 54:

$coordonnees->setVisiteur($visiteur);  

The line below help me to be sure of my data type :

var_dump(gettype($coordonnees));   

I obtain : string(6) "object" which is normal.

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->form = $form;
    $this->request = $request;
    $this->em = $em;
}

public function process()
{

    if ($this->request->getMethod() == 'POST') {
        $this->form->bind($this->request);

        if ($this->form->isValid() ) {
        // echo '<pre>';
        // print_r($this->request);
        // echo '</pre>';
        // die();
        $this->onSuccess($this->form->getData());

        return true;
        }
    }

return false;
}

public function onSuccess(Visiteur $visiteur)
{
    $coordonnees=$visiteur->getCoordonnees();
    $adresse=$visiteur->getAdresse();


    var_dump(gettype($coordonnees));        
    $coordonnees->setVisiteur($visiteur);
    $adresse->setVisiteur($visiteur);


    $this->em->persist($coordonnees);
    $this->em->persist($adresse);

    $visiteur->setCoordonnees($coordonnees);
    $visiteur->setAdresse($adresse);

    $this->em->persist($visiteur);
    $this->em->flush();
}

}

This is my entity visiteur which is my main entity :

<?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->prenom . " " . $this->nom);
}

public function __construct()
{
    $this->createdAt = new \DateTime();
    $this->vehicules = new \Doctrine\Common\Collections\ArrayCollection();
    $this->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 > $this->dateDebut && $maintenant <= $this->dateFin || $this->dateFin == null) return true;
    return false;
}

public function isAncien()
{
    $maintenant = new \DateTime();
    if ($maintenant > $this->dateDebut && $maintenant > $this->dateFin) return true;
    return false;
}

public function isFutur()
{
    $maintenant = new \DateTime();
    if ($maintenant < $this->dateDebut && $maintenant <= $this->dateFin || $this->dateFin == null) return true;
    return false;
}


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

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

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

/**
 * Set prenom
 *
 * @param string $prenom
 */
public function setPrenom($prenom)
{
    $this->prenom = $prenom;
}

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

/**
 * Set dateDebut
 *
 * @param date $dateDebut
 */
public function setDateDebut($dateDebut)
{
    $this->dateDebut = $dateDebut;
}

/**
 * Get dateDebut
 *
 * @return date 
 */
public function getDateDebut()
{
    return $this->dateDebut;
}

/**
 * Set dateFin
 *
 * @param date $dateFin
 */
public function setDateFin($dateFin)
{
    $this->dateFin = $dateFin;
}

/**
 * Get dateFin
 *
 * @return date 
 */
public function getDateFin()
{
    return $this->dateFin;
}

/**
 * Set service
 *
 * @param string $service
 */
public function setService($service)
{
    $this->service = $service;
}

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

/**
 * Set remarqueSecurite
 *
 * @param text $remarqueSecurite
 */
public function setRemarqueSecurite($remarqueSecurite)
{
    $this->remarqueSecurite = $remarqueSecurite;
}

/**
 * Get remarqueSecurite
 *
 * @return text 
 */
public function getRemarqueSecurite()
{
    return $this->remarqueSecurite;
}


/**
 * Add vehicules
 *
 * @param Was\RHBundle\Entity\Vehicule $vehicules
 */
public function addVehicule(\Was\RHBundle\Entity\Vehicule $vehicules)
{
    $this->vehicules[] = $vehicules;
}

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

/**
 * Set coordonnees
 *
 * @param Was\RHBundle\Entity\Coordonnees $coordonnees
 */
public function setCoordonnees(Coordonnees $coordonnees)
{
    $this->coordonnees[] = $coordonnees;
    //$coordonnees->setVisiteur($this);
    //return $this;

}

/**
 * Get coordonnees
 *
 * @return Was\RHBundle\Entity\Coordonnees 
 */
public function getCoordonnees()
{
    return $this->coordonnees;
}

/**
 * Set adresse
 *
 * @param Was\RHBundle\Entity\Adresse $adresse
 */
public function setAdresse(\Was\RHBundle\Entity\Adresse $adresse)
{
    $this->adresse = $adresse;
}

/**
 * Get adresse
 *
 * @return Was\RHBundle\Entity\Adresse 
 */
public function getAdresse()
{
    return $this->adresse;
}

/**
 * Set createdAt
 *
 * @param datetime $createdAt
 */
public function setCreatedAt($createdAt)
{
    $this->createdAt = $createdAt;
}

/**
 * Get createdAt
 *
 * @return datetime 
 */
public function getCreatedAt()
{
    return $this->createdAt;
}

/**
 * Set fonction
 *
 * @param string $fonction
 */
public function setFonction($fonction)
{
    $this->fonction = $fonction;
}

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

/**
 * Set hote
 *
 * @param Was\RHBundle\Entity\Agent $hote
 */
public function setHote(\Was\RHBundle\Entity\Agent $hote)
{
    $this->hote = $hote;
}

/**
 * Get hote
 *
 * @return Was\RHBundle\Entity\Agent 
 */
public function getHote()
{
    return $this->hote;
}

/**
 * Remove vehicules
 *
 * @param Was\RHBundle\Entity\Vehicule $vehicules
 */
public function removeVehicule(\Was\RHBundle\Entity\Vehicule $vehicules)
{
    $this->vehicules->removeElement($vehicules);
}
}

This my coordonnees Entity :

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

/**
 * Set telPro
 *
 * @param string $telPro
 */
public function setTelPro($telPro)
{
    $this->telPro = $telPro;
}

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

/**
 * Set telFax
 *
 * @param string $telFax
 */
public function setTelFax($telFax)
{
    $this->telFax = $telFax;
}

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

/**
 * Set telPortable
 *
 * @param string $telPortable
 */
public function setTelPortable($telPortable)
{
    $this->telPortable = $telPortable;
}

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

/**
 * Set telDomicile
 *
 * @param string $telDomicile
 */
public function setTelDomicile($telDomicile)
{
    $this->telDomicile = $telDomicile;
}

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

/**
 * Set telAutre
 *
 * @param string $telAutre
 */
public function setTelAutre($telAutre)
{
    $this->telAutre = $telAutre;
}

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

/**
 * Set telUrgence
 *
 * @param string $telUrgence
 */
public function setTelUrgence($telUrgence)
{
    $this->telUrgence = $telUrgence;
}

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

/**
 * Set agent
 *
 * @param Was\RHBundle\Entity\Agent $agent
 */
public function setAgent(\Was\RHBundle\Entity\Agent $agent)
{
    $this->agent = $agent;
}

/**
 * Get agent
 *
 * @return Was\RHBundle\Entity\Agent 
 */
public function getAgent()
{
    return $this->agent;
}

/**
 * Set emailPerso
 *
 * @param string $emailPerso
 */
public function setEmailPerso($emailPerso)
{
    $this->emailPerso = $emailPerso;
}

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

/**
 * Set emailPro
 *
 * @param string $emailPro
 */
public function setEmailPro($emailPro)
{
    $this->emailPro = $emailPro;
}

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

/**
 * Set visiteur
 *
 * @param Was\RHBundle\Entity\Visiteur $visiteur
 */
public function setVisiteur(\Was\RHBundle\Entity\Visiteur $visiteur)
{
    $this->visiteur = $visiteur;
}

/**
 * Get visiteur
 *
 * @return Was\RHBundle\Entity\Visiteur 
 */
public function getVisiteur()
{
    return $this->visiteur;
}

/**
 * Set updatedAt
 *
 * @param datetime $updatedAt
 */
public function setUpdatedAt($updatedAt)
{
    $this->updatedAt = $updatedAt;
}

/**
 * Get updatedAt
 *
 * @return datetime 
 */
public function getUpdatedAt()
{
    return $this->updatedAt;
}

/**
 * Set updatedBy
 *
 * @param Was\UserBundle\Entity\User $updatedBy
 */
public function setUpdatedBy($updatedBy)
{
    $this->updatedBy = $updatedBy;
}

/**
 * Get updatedBy
 *
 * @return Was\UserBundle\Entity\User 
 */
public function getUpdatedBy()
{
    return $this->updatedBy;
}

/**
 * Set numeroBadge
 *
 * @param string $numeroBadge
 */
public function setNumeroBadge($numeroBadge)
{
    $this->numeroBadge = $numeroBadge;
}

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

/**
 * Set contactUrgence
 *
 * @param text $contactUrgence
 */
public function setContactUrgence($contactUrgence)
{
    $this->contactUrgence = $contactUrgence;
}

/**
 * Get contactUrgence
 *
 * @return text 
 */
public function getContactUrgence()
{
    return $this->contactUrgence;
}

/**
 * Set contactUrgenceUS
 *
 * @param text $contactUrgenceUS
 */
public function setContactUrgenceUS($contactUrgenceUS)
{
    $this->contactUrgenceUS = $contactUrgenceUS;
}

/**
 * Get contactUrgenceUS
 *
 * @return text 
 */
public function getContactUrgenceUS()
{
    return $this->contactUrgenceUS;
}
}

You say (and define it with annotations) that it's a OneToOne relation. But look in to Visiteur entity class.

You set cordonnees to be an ArrayCollection instead of single Coordonness entity object in constructor.

public function __construct()
{
    $this->createdAt = new \DateTime();
    $this->vehicules = new \Doctrine\Common\Collections\ArrayCollection();
    $this->coordonnees = new \Doctrine\Common\Collections\ArrayCollection(); // <-- here

}

And futher in setter you use it as an array too:

/**
 * Set coordonnees
 *
 * @param Was\RHBundle\Entity\Coordonnees $coordonnees
 */
public function setCoordonnees(Coordonnees $coordonnees)
{
    $this->coordonnees[] = $coordonnees;
    //$coordonnees->setVisiteur($this);
    //return $this;

}

So the getter returns an array (ArrayCollection object actually) of entities.

Let's look at the problematic code:

public function onSuccess(Visiteur $visiteur)
{
    $coordonnees=$visiteur->getCoordonnees(); //this returns ArrayCollection of Coordnnees entity objects
    $adresse=$visiteur->getAdresse();


    var_dump(gettype($coordonnees)); // yes, it says "object" because it's instance of ArrayCollection class
    $coordonnees->setVisiteur($visiteur); //Now you should know, why it won't work. It's not an entity object, but ArrayCollection of entity objects.
    //(...)
}

I think that $coordonneess shoudn't be an ArrayCollection since it's OneToOne relation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM