Tha*_*Pap 5 php entity-relationship doctrine symfony
假设我有一个Post实体和一个Comment实体.管理员可以批准或不批准评论(这是数据库中的标志).邮政实体有:
/**
* @ORM\OneToMany(targetEntity="Comment", mappedBy="post")
*/
protected $comments;
Run Code Online (Sandbox Code Playgroud)
而且我还想要第二个属性,它看起来像:
/**
* @ORM\OneToMany(targetEntity="Comment", mappedBy="post")
*/
protected $approvedComments;
Run Code Online (Sandbox Code Playgroud)
如何在此处仅加载已批准的评论?
您可以使用Inheritance mapping:https://www.doctrine-project.org/projects/doctrine-orm/en/latest/reference/inheritance-mapping.html
这个想法是为每种类型(已批准和未批准)创建单独的类,但将所有内容存储在单个表中(SINGLE_TABLE继承)。
您将需要有额外的列来存储类类型鉴别器。
然后,你会:
/**
* @ORM\OneToMany(targetEntity="ApprovedComment", mappedBy="post")
*/
protected $approvedComments;
/**
* @ORM\OneToMany(targetEntity="NonApprovedComment", mappedBy="post")
*/
protected $nonApprovedComments;
Run Code Online (Sandbox Code Playgroud)
明显的缺点是创建额外的类。
你可以调整你Query/QueryBuilder喜欢:
`SELECT p, c FROM AcmeDemoBundle:Post p LEFT JOIN p.comments c WITH c.approved = FALSE`
Run Code Online (Sandbox Code Playgroud)
这个想法看起来比较合理。