gpt4 book ai didi

php - symfony 错误 : Expected =, <, <=, <>, >, >=, !=, 得到 '.'

转载 作者:行者123 更新时间:2023-12-01 13:51:32 24 4
gpt4 key购买 nike

我有一个查询,用于查找从 Controller 中的变量获得的结果。

$category = $request->request->get('category');

$catContent = $em->getRepository('PublicartelAppBundle:Content')->findByCategory($category);

public function findByCategory($category)
{
$em = $this->getEntityManager();

$dql = 'SELECT c FROM Publicartel\AppBundle\Entity\Content c';

if (!is_null($category)) {
$dql .= " WHERE c.categories.name = category";
}

$query = $this->getEntityManager()
->createQuery($dql)
->setHydrationMode(\Doctrine\ORM\Query::HYDRATE_ARRAY);

if (!is_null($category)) {
$query->setParameter('category', $category);
}

return $query->getResult();
}

但是当我为查询运行操作时,出现此错误:

[Syntax Error] line 0, col 71: Error: Expected =, <, <=, <>, >, >=, !=, got '.'

SELECT c FROM Publicartel\AppBundle\Entity\Content c WHERE c.categories.name = category

$categories 是实体内容的属性。

class Content
{
private $categories;

public function __construct()
{
$this->categories = new \Doctrine\Common\Collections\ArrayCollection();
}

public function addCategories(\Publicartel\AppBundle\Entity\Category $categories)
{
$this->categories[] = $categories;

return $this;
}

public function removeCategories(\Publicartel\AppBundle\Entity\Category $categories)
{
$this->categories->removeElement($categories);
}

public function setCategories(\Publicartel\AppBundle\Entity\Category $categories = null)
{
$this->categories = $categories;

return $this;
}
public function getCategories()
{
return $this->categories;
}
}

奇怪的是,当我获取变量时,并没有出现错误。

$category = $request->query->get('category');

最佳答案

你不能这样引用子对象:

SELECT c FROM Publicartel\AppBundle\Entity\Content c WHERE c.categories.name = :category

您需要在此之前JOIN 并使用该已加入实体的别名。像这样:

SELECT c, cat FROM Publicartel\AppBundle\Entity\Content c JOIN c.categories cat WHERE cat.name = :category

现在,要么使用 LEFT JOIN,要么只使用 JOIN,它应该可以工作。

关于php - symfony 错误 : Expected =, <, <=, <>, >, >=, !=, 得到 '.',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31273457/

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