简体   繁体   中英

bidirectional or unidirectional ManyToOne doctrine2 Association Mapping

Considered this both entities :

    class Comment {
        private $id;
        private $author;
        private $content;

        /**
         * @ORM\ManyToOne(targetEntity="PM\PlatformBundle\Entity\Article")
         * @ORM\JoinColumn(nullable=false)
         */
        private $article;
        //getters setters
    }

    class Article {
        private $id;
        private $author;
        private $content;
        //getters setters
    }

I have chosen ManyToOne relationship between them and unidirectional. But I don't know if bidirectional is great idea for this case or not : For example I have to sort all comments for one article in individual view article. Also in back office (administration), I want be able to sort all comments for one article and remove them if I choose to remove this article. In all other case (comment an article...) uniderctional is great. I know it is possible to make this things entierly with unidirectional method.

What do you recommend ?

It all depends on the use you will have of those entities, but I think it's more useful if the article knows which comments are linked to itself than the other way around. This way, you can easily manipulate an article comments without using a repository, or in a twig template, etc...

And since I'm not really a big fan of using a third table (with the OneToMany unidirectionnal mapping) where the entities two tables do the job, I would recommand using the bidirectionnal mapping.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM