我的Symfony项目有问题。我在Stack Overflow上提问,因为一切对我来说都很好,但它显然不是...... 在我的项目中,我在两个名为" Visiteur"的表之间有一个OneToOne双向Doctrine关系。和" Coordonnees"。
当我提交我的visiteur表格时,我的问题就出现了。要明确这种形式,可以在" visiteur"中保留一些数据。表并且它会在" coordonnees"中保留一些数据。表格("叠加形式"从法语到英语的翻译)
然后我有以下错误:
尝试调用方法" setVisiteur"在课堂上 "学说\共同\类别\ ArrayCollection的"
我的visiteurHandler.php会坚持处理下面的数据。 错误出现在第54行:
$coordonnees->setVisiteur($visiteur);
下面这一行帮助我确定我的数据类型:
var_dump(gettype($coordonnees));
我获得:string(6)" object"这是正常的。
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();
}
}
这是我的实体访问者,这是我的主要实体:
<?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);
}
}
这是我的coordonnees实体:
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;
}
}
答案 0 :(得分:1)
你说(并用注释定义)它是OneToOne关系。
但请查看Visiteur
实体类。
在构造函数中将cordonnees设置为ArrayCollection而不是单个Coordonness实体对象。
public function __construct()
{
$this->createdAt = new \DateTime();
$this->vehicules = new \Doctrine\Common\Collections\ArrayCollection();
$this->coordonnees = new \Doctrine\Common\Collections\ArrayCollection(); // <-- here
}
在setter中你也可以将它用作数组:
/**
* Set coordonnees
*
* @param Was\RHBundle\Entity\Coordonnees $coordonnees
*/
public function setCoordonnees(Coordonnees $coordonnees)
{
$this->coordonnees[] = $coordonnees;
//$coordonnees->setVisiteur($this);
//return $this;
}
因此getter返回实体的数组(实际上是ArrayCollection对象)。
让我们看一下有问题的代码:
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.
//(...)
}
我认为$coordonneess
因为ArrayCollection
OneToOne
关系而感到$scope.sortType = '';
。