Doctrine many to many self referencing with extra columns

馋奶兔 提交于 2019-12-23 05:31:24

问题


i'm working with doctrine & i have some problem in many-to-many self referencing which has some extra fields. lets describe my scenario : i have a table named Drug , drugs can have conflicts with each other & this conflicts may appear under some conditions & may have some solutions.

i've read doctrine documents about many-to-many relations & it's mentioned that it's not good for JoinTable to have some extra fileds. so what's the best solution for this problem?

here is my solution, but i'm not sure that this is the best one or not.

class Drug{
..
/**
 * @var DrugConfilict
 * 
 * @ORM\OneToMany(targetEntity="DrugConfilict", mappedBy="drug1")
 */
private $drugConfilict1s;

/**
 * @var DrugConfilict
 * 
 * @ORM\OneToMany(targetEntity="DrugConfilict", mappedBy="drug2")
 */
private $drugConfilict2s;
}

class DrugConfilict
{
/**
 * @var string
 *
 * @ORM\Column(name="confilict_conditions", type="text", nullable=true)
 */
private $confilictConditions;

/**
 * @var string
 *
 * @ORM\Column(name="what_should_do", type="text", nullable=true)
 */
private $whatShouldDo;

/**
 * @var Drug
 * 
 * @ORM\ManyToOne(targetEntity="Drug", inversedBy="drugConfilict1s")
 * @ORM\JoinColumn(name="drug1_id", referencedColumnName="id")
 */
private $drug1;

/**
 * @var Drug
 * 
 * @ORM\ManyToOne(targetEntity="Drug", inversedBy="drugConfilict2s")
 * @ORM\JoinColumn(name="drug2_id", referencedColumnName="id")
 */
private $drug2;
}

thanks for your answers :)


回答1:


The solution you provided is correct. Perhaps if you can describe the nature of the actual conflict their might be a better data model or data handling for it.



来源:https://stackoverflow.com/questions/18230416/doctrine-many-to-many-self-referencing-with-extra-columns

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!