doctrine2 - Doctrine ORM Conditional Association -



doctrine2 - Doctrine ORM Conditional Association -

i'm building q&a site , questions, answers , comments on same posts table. posttype different. can answers question , comments reply association:

/** * @onetomany(targetentity="cms\entity\post", mappedby="parent") */ private $answers; /** * @onetomany(targetentity="cms\entity\post", mappedby="parent") */ private $comments;

but think not right way because if fetch question both answers , comments filling answers. have set status relation posttype = 1

how can this?

your schema invalid. schould have 2 different objects answers , comments 2 different things, if share mutual interface.

you should create 2 entities, answer , comment , create assocations them. same thing create abstract class, abstractcontent, defines required fields , accessor methods. doctrine supports inheritance final database schema same, oo model correct.

/** * @mappedsuperclass * @inheritancetype("single_table") * @discriminatorcolumn(type = "string", name = "discriminator") * @discriminatormap({ "answer" = "answer", "comment" = "comment" }) */ abstract class abstractcontent { /** @column(type = "integer") @id @generatedvalue("auto") */ protected $id; /** @column(type="text") */ protected $content; /** @column(type = "datetime", name = "created_at") */ protected $createdat; public function __construct() { $this->createdat = new \datetime(); } } /** @entity */ class reply extends abstractcontent { } /** @entity */ class comment extends abstractcontent { } /** * @onetomany(targetentity="cms\entity\answer", mappedby="parent") */ private $answers; /** * @onetomany(targetentity="cms\entity\comment", mappedby="parent") */ private $comments;

you can read more inheritance in doctrine on documentation pages: inheritance mapping

doctrine doctrine2 one-to-many relation

Comments

Popular posts from this blog

iphone - Dismissing a UIAlertView -

intellij idea - Update external libraries with intelij and java -

javascript - send data from a new window to previous window in php -