i'm building a Q&A site and my questions, answers and comments are on the same posts
table. But their postType
is different. I can get answers for a question and comments for an answer with this association:
/**
* @OneToMany(targetEntity="Cms\Entity\Post", mappedBy="parent")
*/
private $answers;
/**
* @OneToMany(targetEntity="Cms\Entity\Post", mappedBy="parent")
*/
private $comments;
But i think this is not the correct way to do this because if i fetch a question both answers and comments are filling with just answers. I have to set a condition for relation like postType = 1
How can i do this?
Use Doctrine's Filtering Collections Criteria class. You can even filter the collection first before the sql query:
Your schema is invalid. You schould have two different objects for answers and comments as they are two different things, even if they share a common interface.
You should create two entities,
Answer
andComment
and create assocations to them. As they are almost the same thing you could create an abstract class,AbstractContent
, that defines all required fields and accessor methods. Doctrine supports inheritance so the final database schema will be exactly the same, but your OO model will be correct.You can read more about inheritance in Doctrine on its documentation pages: Inheritance Mapping