Doctrine - 查询一对多,单向与来自反向的连接表关联

时间:2021-05-19 06:49:26

My target is:

Create universal association where first entity (eg. Category) can be used many times for other Objects (eg. Post, Article)

创建通用关联,其中第一个实体(例如,类别)可以多次用于其他对象(例如,帖子,文章)

Example

Post has categories and Article has categories, but Article and Post are totally different entities. (connection is not possible for both at the same time)

帖子有类别,文章有类别,但文章和帖子是完全不同的实体。 (两者不能同时连接)


Mapping example:

Post

岗位

<?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 One-To-Many, Unidirectional with Join Table association. Now with this I can query for single Post categories and Article categories but Category dont know about Post or Article. This is nice because I can use Category repeatedly.

正如您所看到的,这是一对多,单向与联接表关联。现在,我可以查询单个帖子类别和文章类别,但类别不了解帖子或文章。这很好,因为我可以重复使用Category。


Where is a problem?

I need load ALL Posts or Articles which contain single Category or Categories.

我需要加载包含单个类别或类别的所有帖子或文章。

Example

We have 20 Posts with Category named "symfony" (id:2) and 10 with with Category named "doctrine" (id:3). Now i need query to load all Posts with category "doctrine"

我们有20个帖子,类别名为“symfony”(id:2),10个类别名为“doctrine”(id:3)。现在我需要查询加载所有类别“doctrine”的帖子

findPostsByCategoryId( $id );
// findPostsByCategoryId( 3 );

OR all Posts with both categories

或所有两个类别的帖子

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

How can i do this?

I need solution for this case or solution to achieve my goal. Each tip is appreciated.

我需要针对此案例或解决方案的解决方案来实现我的目标。每个提示都很受欢迎。

P.S. I have other related problem with this mapping described here

附:我在这里描述的映射还有其他相关问题

Validate UniqueEntity for One-To-Many, Unidirectional with Join Table

使用连接表验证One-To-Many,Unidirectional的UniqueEntity

1 个解决方案

#1


6  

Unless I'm misreading your question this seems pretty simple:

除非我误解你的问题,否则这看起来很简单:

Get all posts 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();

#1


6  

Unless I'm misreading your question this seems pretty simple:

除非我误解你的问题,否则这看起来很简单:

Get all posts 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();