doums
doums

Reputation: 450

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 ?

Upvotes: 0

Views: 509

Answers (1)

Boulzy
Boulzy

Reputation: 456

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.

Upvotes: 1

Related Questions