2017-09-08 29 views
0

多重關係我在Symfony的3下面的代碼:Symfony的3 - 爲同一數據模型

Appointment

<?php 

/** 
* Appointment 
* 
* @ORM\Entity 
* @ORM\Table(name="ev_appointment") 
*/ 
class Appointment 
{ 
    /** 
    * @ORM\OneToMany(targetEntity="EmailForward", mappedBy="_appointment") 
    */ 
    private $_email_forwards; 

    /** 
    * @ORM\OneToMany(targetEntity="ParticipationRequest", mappedBy="_appointment") 
    */ 
    private $_participation_requests; 
} 

EmailForward

<?php 
/** 
* @ORM\Entity 
* @ORM\Table(name="ev_email_forward") 
*/ 
class EmailForward 
{ 
    /** 
    * @ORM\ManyToOne(targetEntity="Appointment" , inversedBy="_email_forwards") 
    * @ORM\JoinColumn(name="ev_appointment_id", referencedColumnName="id") 
    */ 
    private $_appointment; 

    /** 
    * @ORM\Column(type="string", length=255, name="email", nullable=true) 
    */ 
    private $_email; 

    /** 
    * @ORM\Column(type="datetime", name="forwarded_at", nullable=true) 
    */ 
    private $_forwarded_at; 

    /** 
    * @ORM\Column(type="string", length=255, name="source", nullable=true) 
    */ 
    private $_source; 
} 

ParticipationRequest

<?php 
/** 
* @ORM\Entity 
* @ORM\Table(name="ev_participation_request") 
*/ 
class ParticipationRequest 
{ 
    /** 
    * @ORM\ManyToOne(targetEntity="Appointment", inversedBy="_participation_requests") 
    * @ORM\JoinColumn(name="ev_appointment_id", referencedColumnName="id") 
    */ 
    private $_appointment; 

    /** 
    * @ORM\Column(type="string", length=255, name="email", nullable=true) 
    */ 
    private $_email; 

    /** 
    * @ORM\Column(type="datetime", name="forwarded_at", nullable=true) 
    */ 
    private $_forwarded_at; 

    /** 
    * @ORM\Column(type="string", length=255, name="source", nullable=true) 
    */ 
    private $_source; 
} 

現在在我看來,我有2個具有完全相同結構的實體的2個關係。所以我想知道什麼是正確的路?

一方面,我可以放棄它,因爲它確實有效。但是,如果兩個領域的某些信息都是相同的,那麼看起來有點浪費,因爲有兩個數據庫條目具有完全相同的信息,並且之後也難以保留。

有沒有更智能的方法來解決這個問題?

+0

看看繼承映射 - 它會幫助你:http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/inheritance-mapping.html – LBA

+0

你應該解釋你用這些對象建模的內容,以及你的應用程序的工作流程是什麼,以獲得更有意義的答案。 –

回答

0

您可以使用例如Single Table Inheritance

由此,您只定義了EmailForwardParticipationRequest的結構,並且所有數據都將保留在數據庫中的一個表中。在ORM映射期間,Doctrine會識別您正在使用的類型併爲您實例化正確的對象。

我不知道如何解決「如果數據是在這兩個關係將peristed兩次相同的」,因爲

  1. ,如果它總是相同的,你不需要兩個關係

  2. 沒有真正的方法來保持它在一個持久性 - 我看到的唯一選擇將創建另一個關係從EmailForwardParticipationRequest保留數據可能需要兩次,並從兩個對象然後引用。