Doctrine is a one-to-many query, one-way with the Join Table association on the back

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 /** @Entity */ class Category { // ... /** * @ORM\Column(type="string", length=100) */ protected $name; } 

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 ); // findPostsByCategoryId( 3 ); 

OR all posts with categories

 findPostsByCategories( Array $array ); // findPostsByCategories( array(2,3) ); 

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

+5
source share
1 answer

If I misunderstand your question, it looks pretty simple:

Receive all messages with a specific category:

 $qb = $this->getEntityManager()->createQueryBuilder(); $qb->select('p') ->from('SomeBundle:Post', 'p') ->join('p.categories', 'c') ->where('c.id = :categoryId') ->setParameter('categoryId', $categoryId) ->getQuery() ->getResult(); 

Get all posts with a range of categories:

 $qb = $this->getEntityManager()->createQueryBuilder(); $qb->select('p') ->from('SomeBundle:Post', 'p') ->join('p.categories', 'c') ->where($qb->expr()->in('c.id', ':categoryIds')) ->setParameter('categoryIds', $categoryIds) // array of ids ->getQuery() ->getResult(); 
+6
source

Source: https://habr.com/ru/post/1240021/


All Articles