gpt4 book ai didi

php - 教义查询多对多[语义错误]

转载 作者:行者123 更新时间:2023-11-29 11:21:21 27 4
gpt4 key购买 nike

目前我有以下 3 个表。

文章类别

+-------------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+---------+------+-----+---------+-------+
| article_id | int(11) | NO | PRI | NULL | |
| category_id | int(11) | NO | PRI | NULL | |
+-------------+---------+------+-----+---------+-------+

类别

+-----------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| title | varchar(255) | NO | | NULL | |
+-----------------+--------------+------+-----+---------+----------------+

文章

+-----------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| title | varchar(255) | NO | | NULL | |
+-----------------+--------------+------+-----+---------+----------------+

在类别实体中

/**
* @ORM\ManyToMany(targetEntity="Article", mappedBy="categories")
* @ORM\OrderBy({"createdAt" = "DESC"})
*/
protected $articles;

文章实体

/**
* All categories this article belongs to.
*
* @ORM\ManyToMany(targetEntity="Category", inversedBy="articles", cascade={"persist"})
* @ORM\JoinTable(name="articles_categories")
*/
protected $categories;

大多数查询都可以正常工作。但我想获取属于某个类别的文章。或者有特定类别或想要根据类别过滤文章。

为此,我编写了以下查询。

$em = $this->getEntityManager();
$qb = $em->createQueryBuilder('c');
$qb->select('1')
->from('articles_categories', 'a_c')
->leftJoin('\\Chip\\Entity\\Article', 'a', 'WITH', 'a.id = a_c.article_id')
->leftJoin('\\Chip\\Entity\\Category', 'c', 'WITH', 'c.id = a_c.category_id')
;

$result = $qb->getQuery()->getResult();

但它抛出以下错误。

[Semantical Error] line 0, col 14 near 'articles_categories': Error: Class 'articles_categories' is not defined.
500 Internal Server Error - QueryException
1 linked Exception: QueryException »

任何帮助或提示或任何更好的编写查询的方法都会很棒。

提前致谢。

最佳答案

您不应在查询生成器中使用表名称,而应使用类名称。

$em = $this->getEntityManager();
$qb = $em->createQueryBuilder('c');
$qb->select('c', 'a')
->from('Chip\Entity\Category', 'c') // this line is not necessary when performing this query in your category repository
->leftJoin('c.articles, a')
->where('c.id = 1');
$result = $qb->getQuery()->getResult();

关于php - 教义查询多对多[语义错误],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38851808/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com