问题
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