My goal:
Create a universal association in which the first object (e.g. Category) can be used many times for other objects (e.g. Post, Article)
Example
Messages have categories, and an article has categories, but article and mail are completely different entities. (connection is not possible for both at the same time)
Matching Example:
Message
<?php /** @Entity */ class Post { // ... /** * @ManyToMany(targetEntity="Category") * @JoinTable(name="post_categories", * joinColumns={@JoinColumn(name="post_id", referencedColumnName="id")}, * inverseJoinColumns={@JoinColumn(name="category_id", referencedColumnName="id", unique=true)} * ) */ private $categories; public function __construct() { $this->categories= new \Doctrine\Common\Collections\ArrayCollection(); } // ... }
Article
<?php /** @Entity */ class Article { // ... /** * @ManyToMany(targetEntity="Category") * @JoinTable(name="article_categories", * joinColumns={@JoinColumn(name="article_id", referencedColumnName="id")}, * inverseJoinColumns={@JoinColumn(name="category_id", referencedColumnName="id", unique=true)} * ) */ private $categories; public function __construct() { $this->categories= new \Doctrine\Common\Collections\ArrayCollection(); } // ... }
Category
<?php class Category {
As you can see, this is a one-way, one-way join of the Join table. Now with this I can query for the individual Post and Article categories, but the Category does not know about the publication or article. This is good because I can use Category several times.
Where is the problem?
I need to download ALL Posts or Articles that contain single Category or Categories .
Example
We have 20 posts with a category named "symfony" (id: 2) and 10 with a category called "doctrine" (id: 3). Now I need a request to download all posts with the "doctrine" category
findPostsByCategoryId( $id );
OR all posts with categories
findPostsByCategories( Array $array );
How can i do this?
I need a solution for this case or a solution to achieve my goal. Every tip is appreciated.
PS I have another problem related to this display described here.
Check unique uniqueness for unidirectional, unidirectional with connection table