symfony - Symfony2 which doctrine relationship for blog between posts and comments -


i building blog symfony2 , wondering doctrine relation mapping should used comments. thing use comments in different parts, commenting on pictures (which not posts) or commenting on comments. need comment entity independent of post.

i wanted try onetomany unidirectionnal forces unique key on post not great.

thanks help

simple way

the simplest way create onetomany relationship each linked entity. it's quite ugly , maybe not effective when searching entities, works.

the mapping similar this:

class comment {     /**      * @orm\manytoone(targetentity="post")      * @orm\joincolumn(nullable=true)      **/     protected $post;      /**      * @orm\manytoone(targetentity="picture")      * @orm\joincolumn(nullable=true)      **/     protected $picture;      /**      * @orm\manytoone(targetentity="comment")      * @orm\joincolumn(nullable=true)      **/     protected $comment; } 

you'll have handle security make sure comment has @ least 1 linked element, , searching might harder, it's basic way it.

complex way

the effective way (but maybe complex) create "discriminant" property , "element-to-be-commented" property, coupled custom doctrine hydrator retrieve objects @ once, each 1 being correct entity.

the "element-to-be-commented" property either comment, picture or post, , discriminant here tell class linked.

in sql terms, means no foreign key between tables, , the element_id dependent of discriminant.


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -