mysql - 为什么 symfony 对连接执行额外的查询

标签 mysql symfony many-to-many

我尝试将数据库中的many2many与带有连接的myisam表连接起来。

我有表article、article_category、actor、article_actor(还有更多类似的many2many关系)。

我的设置

文章:

class Article{
//....

/**
 * @ORM\ManyToMany(targetEntity="Category", inversedBy="articles")
 * @ORM\JoinTable(name="article_category")
 */
protected $categories;

/**
 * @ORM\ManyToMany(targetEntity="Actor", inversedBy="articles")
 * @ORM\JoinTable(name="article_actor")
 */
protected $actors;

/**
 * @ORM\ManyToMany(targetEntity="Cameraman", inversedBy="cameramen")
 * @ORM\JoinTable(name="article_cameraman")
 */
public function __construct() {
  $this->categories = new ArrayCollection();
  $this->actors = new ArrayCollection();
}

类别:

class Category {

/**   
 * @ORM\ManyToMany(targetEntity="Article", mappedBy="categories")
 **/  
protected $articles;       

public function __construct() { 
  $this->articles = new ArrayCollection();
}

Actor :

class Actor {

/**   
 * @ORM\ManyToMany(targetEntity="Article", mappedBy="actors")
 **/  
protected $articles;

public function __construct() {
  $this->articles = new ArrayCollection();
}

在 ArticleRepository 中,我有一个带有连接的查询:

class ArticleRepository extends EntityRepository {
public function findArticleWithJoins($_titleSlug) {

$q = $this->createQueryBuilder('a')
  ->where('a.titleSlug = :titleSlug')
  ->setParameter('titleSlug', $_titleSlug)
  ->leftJoin('a.categories', 'c')
  ->leftJoin('a.actors', 'ac')


return $q->getQuery()->getSingleResult();   

} }

在我的 Controller 中,我获取带有相关连接的文章:

$em = $this->getDoctrine()->getManager();
$article = $em->getRepository('MyBundle:Article')->findArticleWithJoins('theSlug'); 
return $this->render('MyBundle:Default:index.html.twig',array('article' => $article));

最后,当我迭代连接时,模板中就会出现 3 个查询:

{% block body %}
  {{article.title}}<br/>
  {{article.categories.0.name}}<br/>
  {% for actor in article.actors %}
    <li>{{ actor.firstName }} {{ actor.lastName }}</li>
  {% endfor %}
{% endblock %}

第一个查询是:

SELECT 
  a0_.id AS id0,  
  a0_.title AS title15, 
  a0_.orig_title AS orig_title16, 
  a0_.title_slug AS title_slug17, 

FROM 
  article a0_ 
  LEFT JOIN article_category a2_ ON a0_.id = a2_.article_id 
  LEFT JOIN category c1_ ON c1_.id = a2_.category_id 
  LEFT JOIN article_actor a4_ ON a0_.id = a4_.article_id 
  LEFT JOIN actor a3_ ON a3_.id = a4_.actor_id 
WHERE 
  a0_.title_slug = ?

第二个查询:

SELECT 
  t0.id AS id1, 
  t0.name AS name2, 
  t0.slug AS slug3, 
FROM 
  category t0 
  INNER JOIN article_category ON t0.id = article_category.category_id 
WHERE 
  article_category.article_id = ?

第三个查询:

SELECT 
  t0.id AS id1, 
  t0.first_name AS first_name2, 
  t0.last_name AS last_name3, 
  t0.name_slug AS name_slug4, 
FROM 
  actor t0 
  INNER JOIN article_actor ON t0.id = article_actor.actor_id 
WHERE 
  article_actor.article_id = ?

我想通过使用 findArticleWithJoins() 函数中的联接来避免多次查询。 但结果是一样的,只是查询文章,然后再选择关系。

我做错了什么吗?

最佳答案

将 select (->select('a, c, ac') 添加到您的查询中:

$q = $this->createQueryBuilder('a')
  ->select('a, c, ac')
  ->where('a.titleSlug = :titleSlug')
  ->setParameter('titleSlug', $_titleSlug)
  ->leftJoin('a.categories', 'c')
  ->leftJoin('a.actors', 'ac')

这将使学说能够用关系来构造对象。

关于mysql - 为什么 symfony 对连接执行额外的查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24911164/

相关文章:

Mysql多对多关系获取 'and'和 'not'数据的数据

java - JPA ManyToMany 映射问题(无法将同一实体映射到另一个实体)

java - EclipseLink级联持续异常

php - 登录凭据问题 - 远程 mysql 服务器(错误 2002)

symfony - Symfony 中的功能测试

php - Symfony 2 上的 Endroid/Qrcode 和 __DIR__ const 出现内部服务器错误

symfony - Doctrine left join wherejoined table is null 显示结果

mysql - 1对多选择并连接

MySql 分组并按年周计数

mysql - 使用 group by with where 子句时出错