gpt4 book ai didi

Symfony 3 参数过多

转载 作者:行者123 更新时间:2023-12-03 00:29:34 27 4
gpt4 key购买 nike

我是 Symfony 新手,在运行查询时遇到错误:

public function getFilteredArticles($page, $nbPerPage, $data) {
$query = $this->createQueryBuilder('a')
->leftJoin('a.images', 'i')
->addSelect('i')
->leftJoin('a.type_stockage', 't')
->addSelect('t')
->leftJoin('a.famille', 'f')
->addSelect('f');
if ($data['famille'] != '') {
$query->where('f.id = :famille')
->setParameter('famille', $data['famille']);
}
if ($data['rds'] == false) {
$query->where('a.stock_actuel > 0');
}
if ($data['recherche'] != '' && $data['recherche'] != null) {
$query->where('a.ref_article LIKE :recherche')
->setParameter('recherche', '%' . $data['recherche'] . '%');
}
$query->leftJoin('a.sousfamille', 's')
->orderBy('a.ref_article', 'ASC')
->getQuery();

$query->setFirstResult(($page - 1) * $nbPerPage)
->setMaxResults($nbPerPage);

return new Paginator($query, true);
}

如您所见,此查询具有条件参数,它返回表所需的文章列表。但是当我运行此查询来填充我的表时,我收到错误:

An exception has been thrown during the rendering of a template ("Too many parameters: the query defines 0 parameters and you bound 1").

我不知道为什么他需要 0 个参数。我尝试使用 setParameters 代替,但结果是相同的。

有人有想法吗?

最佳答案

您应该使用 andWhere() 方法而不是 where()
where() 方法会删除所有先前的 where,但 setParameter() 不会。这就是为什么他发现了比 where 子句更多的参数。

如果条件没有意义作为第一个条件,我个人从不使用 where ,以避免此类错误。

    if ($data['famille'] != '') {
$query->andWhere('f.id = :famille')
->setParameter('famille', $data['famille']);
}
if ($data['rds'] == false) {
$query->andWhere('a.stock_actuel > 0');
}
if ($data['recherche'] != '' && $data['recherche'] != null) {
$query->andWhere('a.ref_article LIKE :recherche')
->setParameter('recherche', '%' . $data['recherche'] . '%');
}

where() PHP 文档

Specifies one or more restrictions to the query result.
Replaces any previously specified restrictions, if any.

andWhere() PHP 文档

Adds one or more restrictions to the query results, forming a logical conjunction with any previously specified restrictions.

关于Symfony 3 参数过多,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44392390/

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