php - Symfony2 Error: Attempted to call method on class "Doctrine\Common\Collections\ArrayCollection" -
i have problem symfony project. ask question on stack overflow because seems me it's apparently not... in project have onetoone bidirectional doctrine relation between 2 table named "visiteur" , "coordonnees".
my problem appears when submit visiteur form. clear form persist data in "visiteur" table , persist data in "coordonnees" table ("imbricated form" translation french english)
then have error below :
attempted call method "setvisiteur" on class "doctrine\common\collections\arraycollection".
there visiteurhandler.php persist handle data below. error appears in line 54:
$coordonnees->setvisiteur($visiteur);
the line below me sure of data type :
var_dump(gettype($coordonnees));
i obtain : string(6) "object" 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 entity visiteur main entity :
<?php namespace was\rhbundle\entity; use doctrine\orm\mapping orm; use symfony\component\validator\constraints 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; } /** * id * * @return integer */ public function getid() { return $this->id; } /** * set nom * * @param string $nom */ public function setnom($nom) { $this->nom = $nom; } /** * nom * * @return string */ public function getnom() { return $this->nom; } /** * set prenom * * @param string $prenom */ public function setprenom($prenom) { $this->prenom = $prenom; } /** * prenom * * @return string */ public function getprenom() { return $this->prenom; } /** * set datedebut * * @param date $datedebut */ public function setdatedebut($datedebut) { $this->datedebut = $datedebut; } /** * datedebut * * @return date */ public function getdatedebut() { return $this->datedebut; } /** * set datefin * * @param date $datefin */ public function setdatefin($datefin) { $this->datefin = $datefin; } /** * datefin * * @return date */ public function getdatefin() { return $this->datefin; } /** * set service * * @param string $service */ public function setservice($service) { $this->service = $service; } /** * service * * @return string */ public function getservice() { return $this->service; } /** * set remarquesecurite * * @param text $remarquesecurite */ public function setremarquesecurite($remarquesecurite) { $this->remarquesecurite = $remarquesecurite; } /** * 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; } /** * 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; } /** * 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; } /** * adresse * * @return was\rhbundle\entity\adresse */ public function getadresse() { return $this->adresse; } /** * set createdat * * @param datetime $createdat */ public function setcreatedat($createdat) { $this->createdat = $createdat; } /** * createdat * * @return datetime */ public function getcreatedat() { return $this->createdat; } /** * set fonction * * @param string $fonction */ public function setfonction($fonction) { $this->fonction = $fonction; } /** * 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; } /** * 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 coordonnees entity :
namespace was\rhbundle\entity; use doctrine\orm\mapping orm; use symfony\component\validator\constraints assert; use symfony\bridge\doctrine\validator\constraints\uniqueentity; use was\userbundle\entity\user 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; /** * id * * @return integer */ public function getid() { return $this->id; } /** * set telpro * * @param string $telpro */ public function settelpro($telpro) { $this->telpro = $telpro; } /** * telpro * * @return string */ public function gettelpro() { return $this->telpro; } /** * set telfax * * @param string $telfax */ public function settelfax($telfax) { $this->telfax = $telfax; } /** * telfax * * @return string */ public function gettelfax() { return $this->telfax; } /** * set telportable * * @param string $telportable */ public function settelportable($telportable) { $this->telportable = $telportable; } /** * telportable * * @return string */ public function gettelportable() { return $this->telportable; } /** * set teldomicile * * @param string $teldomicile */ public function setteldomicile($teldomicile) { $this->teldomicile = $teldomicile; } /** * teldomicile * * @return string */ public function getteldomicile() { return $this->teldomicile; } /** * set telautre * * @param string $telautre */ public function settelautre($telautre) { $this->telautre = $telautre; } /** * telautre * * @return string */ public function gettelautre() { return $this->telautre; } /** * set telurgence * * @param string $telurgence */ public function settelurgence($telurgence) { $this->telurgence = $telurgence; } /** * 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; } /** * agent * * @return was\rhbundle\entity\agent */ public function getagent() { return $this->agent; } /** * set emailperso * * @param string $emailperso */ public function setemailperso($emailperso) { $this->emailperso = $emailperso; } /** * emailperso * * @return string */ public function getemailperso() { return $this->emailperso; } /** * set emailpro * * @param string $emailpro */ public function setemailpro($emailpro) { $this->emailpro = $emailpro; } /** * 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; } /** * visiteur * * @return was\rhbundle\entity\visiteur */ public function getvisiteur() { return $this->visiteur; } /** * set updatedat * * @param datetime $updatedat */ public function setupdatedat($updatedat) { $this->updatedat = $updatedat; } /** * updatedat * * @return datetime */ public function getupdatedat() { return $this->updatedat; } /** * set updatedby * * @param was\userbundle\entity\user $updatedby */ public function setupdatedby($updatedby) { $this->updatedby = $updatedby; } /** * updatedby * * @return was\userbundle\entity\user */ public function getupdatedby() { return $this->updatedby; } /** * set numerobadge * * @param string $numerobadge */ public function setnumerobadge($numerobadge) { $this->numerobadge = $numerobadge; } /** * numerobadge * * @return string */ public function getnumerobadge() { return $this->numerobadge; } /** * set contacturgence * * @param text $contacturgence */ public function setcontacturgence($contacturgence) { $this->contacturgence = $contacturgence; } /** * contacturgence * * @return text */ public function getcontacturgence() { return $this->contacturgence; } /** * set contacturgenceus * * @param text $contacturgenceus */ public function setcontacturgenceus($contacturgenceus) { $this->contacturgenceus = $contacturgenceus; } /** * contacturgenceus * * @return text */ public function getcontacturgenceus() { return $this->contacturgenceus; } }
you (and define annotations) it's onetoone relation. in visiteur
entity class.
you set cordonnees 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 use array too:
/** * set coordonnees * * @param was\rhbundle\entity\coordonnees $coordonnees */ public function setcoordonnees(coordonnees $coordonnees) { $this->coordonnees[] = $coordonnees; //$coordonnees->setvisiteur($this); //return $this; }
so getter returns array (arraycollection object actually) of entities.
let's @ 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, says "object" because it's instance of arraycollection class $coordonnees->setvisiteur($visiteur); //now should know, why won't work. it's not entity object, arraycollection of entity objects. //(...) }
i think $coordonneess
shoudn't arraycollection
since it's onetoone
relation.
Comments
Post a Comment