2014-10-03 99 views
1

我有我的数据库中的两个表: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; 
+0

你只应该需要'mappedBy'的一对多关系(其中表示其中外键存储在“一对多”领域你只需要'inversedBy'来获得双向关系(例如,你想能够找到RefParam所有的声明)。例如,我不希望看到一个简单的ManyToOne的错误消息,就像你说的那样应该可以'$ declaration-> setStatus(null)'。你有RefParam上配置的其他关联吗? – frumious 2014-10-03 12:02:34

+0

我们能否看到你的持久性代码和完整的实体? – frumious 2014-10-03 12:05:10

+0

absolutly ...在声明.php我有三个属性是RefParam对象 – mlwacosmos 2014-10-03 12:06:17

回答

0

问题是此行fromModelClass当你得到RefParam对象:

$refParamStatut = new \Cramif\AccueilBundle\Entity\RefParam(); 

从教义的角度来看statut被设置为一个全新的对象,从来没有见过的。当你坚持$declaration_entity它知道它的RefParam是一个实体,但没有一个在数据库中,所以它抱怨。你或者需要自己坚持下去,或者坚持级联,所以教义为你做,或者,这可能是你的情况的答案,因为你不想做任何新的RefParam s,从正确的RefParam对象数据库首先(然后它不需要坚持)。

例如

fromModelClass

$refParamStatut = $paramRepository->find($declaration->getStatut()->getId()); 

持久(NB现在从你的例子不变,因为$申述> 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 
0

您分配符合新的对象:

$declaration->setStatut(new Param(2)); 

这不是由Doctrine管理,这就是为什么它想坚持它。

而不是使用一个从数据库中取出的,你可以设置参考:

$declaration->setStatut($em->getReference('App\AcmeBUndle\Entity\Param', 2));