多对多的质疑主义[语义学错误]

目前我有以下3个表。

articles_categories

+-------------+---------+------+-----+---------+-------+
| 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 ? 

任何帮助或提示,或者任何更好的编写查询的方法都是很好的。

提前谢谢。

转载请注明出处:http://www.tochigihk.com/article/20230526/1225524.html