我有我的數據庫中的兩個表:Symfony的2:理論和相關實體
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;
}
}
當堅持宣言的對象,我有一個例外:
A new entity was found that was not configured to cascade persist operations for entity RefParam
當然,我不想堅持Ref參數,因爲所有的狀態都已經在數據庫中。 所以,我不確定我是否正確,我想過使用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"
它看起來更reversedBy,我可以多對一的關係使用。它不能解決我的問題。
那麼我的問題是:
如何堅持宣言對象,並把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;
你只應該需要'mappedBy'的一對多關係(其中表示其中外鍵存儲在「一對多」領域你只需要'inversedBy'來獲得雙向關係(例如,你想能夠找到RefParam所有的聲明)。例如,我不希望看到一個簡單的ManyToOne的錯誤消息,就像你說的那樣應該可以'$ declaration-> setStatus(null)'。你有RefParam上配置的其他關聯嗎? – frumious 2014-10-03 12:02:34
我們能否看到你的持久性代碼和完整的實體? – frumious 2014-10-03 12:05:10
absolutly ...在聲明.php我有三個屬性是RefParam對象 – mlwacosmos 2014-10-03 12:06:17