[英]Doctrine - Query One-To-Many, Unidirectional with Join Table association from inversed side
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)
(两者不能同时连接)
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. 正如您所看到的,这是One-To-Many,单向连接表关联。 Now with this I can query for single
Post
categories and Article
categories but Category dont know about Post or Article. 现在,我可以查询单个
Post
类别和Article
类别,但类别不了解帖子或文章。 This is nice because I can use Category
repeatedly. 这很好,因为我可以重复使用
Category
。
I need load ALL Posts
or Articles
which contain single Category
or Categories
. 我需要加载包含单个
Category
或Categories
所有 Posts
或Articles
。
Example 例
We have 20 Posts with Category named "symfony"
(id:2) and 10 with with Category named "doctrine"
(id:3) . 我们有20个帖子,类别名为
"symfony"
(id:2) ,10个名称为"doctrine"
(id:3) 。 Now i need query to load all Posts with category "doctrine"
现在我需要查询加载所有类别
"doctrine"
帖子
findPostsByCategoryId( $id );
// findPostsByCategoryId( 3 );
OR all Posts with both categories 或所有两个类别的帖子
findPostsByCategories( Array $array );
// findPostsByCategories( array(2,3) );
I need solution for this case or solution to achieve my goal. 我需要针对此案例或解决方案的解决方案来实现我的目标。 Each tip is appreciated.
每个提示都很受欢迎。
PS I have other related problem with this mapping described here PS我在这里描述的映射还有其他相关问题
Validate UniqueEntity for One-To-Many, Unidirectional with Join Table 使用连接表验证One-To-Many,Unidirectional的UniqueEntity
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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.