我的数据库中有这两个表:
REF_PARAM(id, label)
DECLARATION(number, status)
'状态'是REF_PARAM的外键。
我用命令行生成我的实体。我有2个实体RefParam和声明:
<?php
namespace Cramif\AccueilBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Declaration
*
* @ORM\Table(name="declaration")
* @ORM\Entity(repositoryClass="Cramif\AccueilBundle\Repository\DeclarationRepository")
*/
class Declaration
{
/**
* @var string
*
* @ORM\Column(name="IDENTIFIANT", type="string", length=8, nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $identifiant;
/**
* @var string
*
* @ORM\Column(name="NOM_INTERLOCUTEUR", type="string", length=50, nullable=true)
*/
private $nomInterlocuteur;
/**
* @var string
*
* @ORM\Column(name="REFERENCE", type="string", length=14, nullable=true)
*/
private $reference;
/**
* @var string
*
* @ORM\Column(name="DESCRIPTION", type="text", nullable=true)
*/
private $description;
/**
* @var string
*
* @ORM\Column(name="COMMENTAIRE", type="string", length=255, nullable=true)
*/
private $commentaire;
/**
* @var string
*
* @ORM\Column(name="AGENT_SAISIE_ID", type="string", length=6, nullable=true)
*/
private $agentSaisieId;
/**
* @var string
*
* @ORM\Column(name="AGENT_SAISIE_NOM", type="string", length=50, nullable=true)
*/
private $agentSaisieNom;
/**
* @var string
*
* @ORM\Column(name="AGENT_SAISIE_PRENOM", type="string", length=50, nullable=true)
*/
private $agentSaisiePrenom;
/**
* @var \DateTime
*
* @ORM\Column(name="DATE_CREATION", type="date", nullable=true)
*/
private $dateCreation;
/**
* @var string
*
* @ORM\Column(name="AGENT_VALIDATION_ID", type="string", length=6, nullable=true)
*/
private $agentValidationId;
/**
* @var string
*
* @ORM\Column(name="AGENT_VALIDATION_NOM", type="string", length=50, nullable=true)
*/
private $agentValidationNom;
/**
* @var string
*
* @ORM\Column(name="AGENT_VALIDATION_PRENOM", type="string", length=50, nullable=true)
*/
private $agentValidationPrenom;
/**
* @var \DateTime
*
* @ORM\Column(name="DATE_VALIDATION", type="date", nullable=true)
*/
private $dateValidation;
/**
* @var \DateTime
*
* @ORM\Column(name="DATE_FERMETURE", type="date", nullable=true)
*/
private $dateFermeture;
/**
* @var \RefParam
*
* @ORM\ManyToOne(targetEntity="RefParam")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="TRAITEMENT", referencedColumnName="ID")
* })
*/
private $traitement;
/**
* @var \RefParam
*
* @ORM\ManyToOne(targetEntity="RefParam")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="STATUT", referencedColumnName="ID")
* })
*/
private $statut;
/**
* @var \RefParam
*
* @ORM\ManyToOne(targetEntity="RefParam")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="STATUT_INTERLOCUTEUR", referencedColumnName="ID")
* })
*/
private $statutInterlocuteur;
/**
* Get identifiant
*
* @return string
*/
public function getIdentifiant()
{
return $this->identifiant;
}
/**
* Get identifiant
*
* @return string
*/
public function setIdentifiant($identifiant)
{
$this->identifiant = $identifiant;
return $this;
}
/**
* Set nomInterlocuteur
*
* @param string $nomInterlocuteur
* @return Declaration
*/
public function setNomInterlocuteur($nomInterlocuteur)
{
$this->nomInterlocuteur = $nomInterlocuteur;
return $this;
}
/**
* Get nomInterlocuteur
*
* @return string
*/
public function getNomInterlocuteur()
{
return $this->nomInterlocuteur;
}
/**
* Set reference
*
* @param string $reference
* @return Declaration
*/
public function setReference($reference)
{
$this->reference = $reference;
return $this;
}
/**
* Get reference
*
* @return string
*/
public function getReference()
{
return $this->reference;
}
/**
* Set description
*
* @param string $description
* @return Declaration
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}
/**
* Get description
*
* @return string
*/
public function getDescription()
{
return $this->description;
}
/**
* Set commentaire
*
* @param string $commentaire
* @return Declaration
*/
public function setCommentaire($commentaire)
{
$this->commentaire = $commentaire;
return $this;
}
/**
* Get commentaire
*
* @return string
*/
public function getCommentaire()
{
return $this->commentaire;
}
/**
* Set agentSaisieId
*
* @param string $agentSaisieId
* @return Declaration
*/
public function setAgentSaisieId($agentSaisieId)
{
$this->agentSaisieId = $agentSaisieId;
return $this;
}
/**
* Get agentSaisieId
*
* @return string
*/
public function getAgentSaisieId()
{
return $this->agentSaisieId;
}
/**
* Set agentSaisieNom
*
* @param string $agentSaisieNom
* @return Declaration
*/
public function setAgentSaisieNom($agentSaisieNom)
{
$this->agentSaisieNom = $agentSaisieNom;
return $this;
}
/**
* Get agentSaisieNom
*
* @return string
*/
public function getAgentSaisieNom()
{
return $this->agentSaisieNom;
}
/**
* Set agentSaisiePrenom
*
* @param string $agentSaisiePrenom
* @return Declaration
*/
public function setAgentSaisiePrenom($agentSaisiePrenom)
{
$this->agentSaisiePrenom = $agentSaisiePrenom;
return $this;
}
/**
* Get agentSaisiePrenom
*
* @return string
*/
public function getAgentSaisiePrenom()
{
return $this->agentSaisiePrenom;
}
/**
* Set dateCreation
*
* @param \DateTime $dateCreation
* @return Declaration
*/
public function setDateCreation($dateCreation)
{
$this->dateCreation = $dateCreation;
return $this;
}
/**
* Get dateCreation
*
* @return \DateTime
*/
public function getDateCreation()
{
return $this->dateCreation;
}
/**
* Set agentValidationId
*
* @param string $agentValidationId
* @return Declaration
*/
public function setAgentValidationId($agentValidationId)
{
$this->agentValidationId = $agentValidationId;
return $this;
}
/**
* Get agentValidationId
*
* @return string
*/
public function getAgentValidationId()
{
return $this->agentValidationId;
}
/**
* Set agentValidationNom
*
* @param string $agentValidationNom
* @return Declaration
*/
public function setAgentValidationNom($agentValidationNom)
{
$this->agentValidationNom = $agentValidationNom;
return $this;
}
/**
* Get agentValidationNom
*
* @return string
*/
public function getAgentValidationNom()
{
return $this->agentValidationNom;
}
/**
* Set agentValidationPrenom
*
* @param string $agentValidationPrenom
* @return Declaration
*/
public function setAgentValidationPrenom($agentValidationPrenom)
{
$this->agentValidationPrenom = $agentValidationPrenom;
return $this;
}
/**
* Get agentValidationPrenom
*
* @return string
*/
public function getAgentValidationPrenom()
{
return $this->agentValidationPrenom;
}
/**
* Set dateValidation
*
* @param \DateTime $dateValidation
* @return Declaration
*/
public function setDateValidation($dateValidation)
{
$this->dateValidation = $dateValidation;
return $this;
}
/**
* Get dateValidation
*
* @return \DateTime
*/
public function getDateValidation()
{
return $this->dateValidation;
}
/**
* Set dateFermeture
*
* @param \DateTime $dateFermeture
* @return Declaration
*/
public function setDateFermeture($dateFermeture)
{
$this->dateFermeture = $dateFermeture;
return $this;
}
/**
* Get dateFermeture
*
* @return \DateTime
*/
public function getDateFermeture()
{
return $this->dateFermeture;
}
/**
* Set traitement
*
* @param \Cramif\AccueilBundle\Entity\RefParam $traitement
* @return Declaration
*/
public function setTraitement(\Cramif\AccueilBundle\Entity\RefParam $traitement = null)
{
$this->traitement = $traitement;
return $this;
}
/**
* Get traitement
*
* @return \Cramif\AccueilBundle\Entity\RefParam
*/
public function getTraitement()
{
return $this->traitement;
}
/**
* Set statut
*
* @param \Cramif\AccueilBundle\Entity\RefParam $statut
* @return Declaration
*/
public function setStatut(\Cramif\AccueilBundle\Entity\RefParam $statut = null)
{
$this->statut = $statut;
return $this;
}
/**
* Get statut
*
* @return \Cramif\AccueilBundle\Entity\RefParam
*/
public function getStatut()
{
return $this->statut;
}
/**
* Set statutInterlocuteur
*
* @param \Cramif\AccueilBundle\Entity\RefParam $statutInterlocuteur
* @return Declaration
*/
public function setStatutInterlocuteur(\Cramif\AccueilBundle\Entity\RefParam $statutInterlocuteur = null)
{
$this->statutInterlocuteur = $statutInterlocuteur;
return $this;
}
/**
* Get statutInterlocuteur
*
* @return \Cramif\AccueilBundle\Entity\RefParam
*/
public function getStatutInterlocuteur()
{
return $this->statutInterlocuteur;
}
}
namespace Cramif\AccueilBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* RefParam
*
* @ORM\Table(name="ref_param")
* @ORM\Entity(repositoryClass="Cramif\AccueilBundle\Repository\RefParamRepository")
*/
class RefParam
{
/**
* @var integer
*
* @ORM\Column(name="ID", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="LIBELLE", type="string", length=50, nullable=true)
*/
private $libelle;
/**
* @var \DateTime
*
* @ORM\Column(name="DATE_CREATION", type="date", nullable=true)
*/
private $dateCreation;
/**
* @var \DateTime
*
* @ORM\Column(name="DATE_MODIFICATION", type="date", nullable=true)
*/
private $dateModification;
/**
* @var \DateTime
*
* @ORM\Column(name="DATE_SUPPRESSION", type="date", nullable=true)
*/
private $dateSuppression;
/**
* @var \RefCategorie
*
* @ORM\ManyToOne(targetEntity="RefCategorie")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="ID_CATEGORIE", referencedColumnName="ID")
* })
*/
private $idCategorie;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
public function setId($id) {
$this->id = $id;
return $this;
}
/**
* Set libelle
*
* @param string $libelle
* @return RefParam
*/
public function setLibelle($libelle)
{
$this->libelle = $libelle;
return $this;
}
/**
* Get libelle
*
* @return string
*/
public function getLibelle()
{
return $this->libelle;
}
/**
* Set dateCreation
*
* @param \DateTime $dateCreation
* @return RefParam
*/
public function setDateCreation($dateCreation)
{
$this->dateCreation = $dateCreation;
return $this;
}
/**
* Get dateCreation
*
* @return \DateTime
*/
public function getDateCreation()
{
return $this->dateCreation;
}
/**
* Set dateModification
*
* @param \DateTime $dateModification
* @return RefParam
*/
public function setDateModification($dateModification)
{
$this->dateModification = $dateModification;
return $this;
}
/**
* Get dateModification
*
* @return \DateTime
*/
public function getDateModification()
{
return $this->dateModification;
}
/**
* Set dateSuppression
*
* @param \DateTime $dateSuppression
* @return RefParam
*/
public function setDateSuppression($dateSuppression)
{
$this->dateSuppression = $dateSuppression;
return $this;
}
/**
* Get dateSuppression
*
* @return \DateTime
*/
public function getDateSuppression()
{
return $this->dateSuppression;
}
/**
* Set idCategorie
*
* @param \Cramif\AccueilBundle\Entity\RefCategorie $idCategorie
* @return RefParam
*/
public function setIdCategorie(\Cramif\AccueilBundle\Entity\RefCategorie $idCategorie = null)
{
$this->idCategorie = $idCategorie;
return $this;
}
/**
* Get idCategorie
*
* @return \Cramif\AccueilBundle\Entity\RefCategorie
*/
public function getIdCategorie()
{
return $this->idCategorie;
}
}
当持久化Statement对象时,我有一个例外:
A new entity was found that was not configured to cascade persist operations for entity RefParam
当然,我不想坚持使用RefParam,因为所有状态都已存在于数据库中。 所以,我不确定我是对的,我想使用mappedBy属性来表示我想在DECLARATION表中保留声明时使用RefParam的id。 所以我补充道:
/**
* @var \RefParam
*
* @ORM\ManyToOne(targetEntity="RefParam", mappedBy="id")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="STATUS", referencedColumnName="ID")
* })
*/
我有另一个例外:
The annotation @ORM\ManyToOne does not have a property named "mappedBy"
看起来它更倾向于我可以使用ManyToOne关系。它没有解决我的问题。
我的问题是:
如何持久化Statement对象并将RefParam对象的id放在状态列中?
谢谢你
PERSISTENCE
$declaration = new Declaration(); // this is not the entity class but a Model class I have
$declaration->setDeclarationForm($declaration_form); //declaration_form is the object the form is based on
$declaration->setDateCreation(new \DateTime());
$declaration->setAgentSaisie($session->get('agent'));
$declaration->setStatut(new Param(2));
$declaration_entity = DeclarationRepository::fromModelClass($declaration); // get the entity class Declaration from the model class I have
$em = $this->getDoctrine()->getManager();
$em->persist($declaration_entity);
$em->flush();
模型到实体转换
public static function fromModelClass(\Cramif\AccueilBundle\Model\Declaration $declaration) {
$entity_declaration = new \Cramif\AccueilBundle\Entity\Declaration();
$entity_declaration->setIdentifiant($declaration->getDeclarationForm()->getIdentifiant());
$entity_declaration->setNomInterlocuteur($declaration->getDeclarationForm()->getNomCible());
$entity_declaration->setReference($declaration->getDeclarationForm()->getReference());
$entity_declaration->setDescription($declaration->getDeclarationForm()->getDescription());
$entity_declaration->setCommentaire($declaration->getDeclarationForm()->getCommentaire());
$entity_declaration->setDateCreation($declaration->getDateCreation());
$entity_declaration->setDateFermeture($declaration->getDateFermeture());
$entity_declaration->setDateValidation($declaration->getDateValidation());
$entity_declaration->setAgentSaisieId($declaration->getAgentSaisie()->getIdentifiant());
$entity_declaration->setAgentSaisieNom($declaration->getAgentSaisie()->getNom());
$entity_declaration->setAgentSaisiePrenom($declaration->getAgentSaisie()->getPrenom());
if(is_object($declaration->getAgentValidation())) {
$entity_declaration->setAgentValidationId($declaration->getAgentValidation()->getIdentifiant());
$entity_declaration->setAgentValidationNom($declaration->getAgentValidation()->getNom());
$entity_declaration->setAgentValidationPrenom($declaration->getAgentValidation()->getPrenom());
}
$refParamStatut = new \Cramif\AccueilBundle\Entity\RefParam();
$refParamStatut->setId($declaration->getStatut()->getId());
$entity_declaration->setStatut($refParamStatut);
$refParamTraitement = new \Cramif\AccueilBundle\Entity\RefParam();
$refParamTraitement->setId($declaration->getDeclarationForm()->getTraitement()->getId());
$entity_declaration->setTraitement($refParamTraitement);
$refParamStatutInterlocuteur = new \Cramif\AccueilBundle\Entity\RefParam();
$refParamStatutInterlocuteur->setId($declaration->getDeclarationForm()->getQualiteInterlocuteur()->getId());
$entity_declaration->setStatutInterlocuteur($refParamStatutInterlocuteur);
return $entity_declaration;
答案 0 :(得分:0)
当您获得fromModelClass
对象时,RefParam
中的问题就出现了这一行:
$refParamStatut = new \Cramif\AccueilBundle\Entity\RefParam();
从Doctrine的角度来看,statut
被设置为一个前所未有的全新对象。当你持久$declaration_entity
时,它知道它的RefParam
是一个实体而不是数据库中的实体,所以它会抱怨。你要么自己坚持下去,要么坚持级联,所以Doctrine会为你做,或者,这可能是你的情况下的答案,因为你不想做任何新的RefParam
,取得正确的首先是来自DB的RefParam
对象(然后它根本不需要持久化)。
e.g。
fromModelClass
$refParamStatut = $paramRepository->find($declaration->getStatut()->getId());
持久性(NB现在与您的示例相同,因为$ declaration-&gt; refParamStatut是一个托管实体)
$param = new Param(2); //POPO model
$declaration->setStatut($param);
//Within fromModelClass $param gets turned into a managed RefParam Entity, which doesn't need persisting
$declaration_entity = DeclarationRepository::fromModelClass($declaration);
$em = $this->getDoctrine()->getManager();
$em->persist($declaration_entity);
$em->flush(); //No exception
答案 1 :(得分:0)
您在线分配新对象:
$declaration->setStatut(new Param(2));
这不是由Doctrine管理的,这就是为什么它要坚持下去。
您可以设置参考:
,而不是使用从DB中提取的数据$declaration->setStatut($em->getReference('App\AcmeBUndle\Entity\Param', 2));