Conditional Relationship in Symfony2
doctrine, entity-relationship, php, symfony
Solution
Idea #1
You could use `Inheritance mapping`: https://www.doctrine-project.org/projects/doctrine-orm/en/latest/reference/inheritance-mapping.html
The idea would to have separate classes for each type (approved and non-approved), but to store everything in a single table (`SINGLE_TABLE` inheritance).
You will need to have additional column which will store class type discriminator.
Then, you would have:
/**
* @ORM\OneToMany(targetEntity="ApprovedComment", mappedBy="post")
*/
protected $approvedComments;
/**
* @ORM\OneToMany(targetEntity="NonApprovedComment", mappedBy="post")
*/
protected $nonApprovedComments;
The obvious downside is creation of additional classes.
Idea #2
You could just tweak you `Query`/`QueryBuilder` like:
`SELECT p, c FROM AcmeDemoBundle:Post p LEFT JOIN p.comments c WITH c.approved = FALSE`
This idea seems more reasonable.
Problem
Let's say I have a Post entity, and a Comment entity. A comment can be approved or not by an admin (which is a flag in the db). The post entity has: ``` /** * @ORM\OneToMany(targetEntity="Comment", mappedBy="post") */ protected $comments; ``` And I also want a second attribute which will look like: ``` /** * @ORM\OneToMany(targetEntity="Comment", mappedBy="post") */ protected $approvedComments; ``` How is it possible to load only the approved comments here?