在我一直在研究的 Symfony2 CRM 中执行搜索时,我收到上述错误。根据谷歌搜索,这似乎是一个与 KNP Paginator 捆绑包有关的问题,但我似乎找不到可靠的解决方案。
在本例中,我使用来自 OpenCart 数据库的数据,我需要能够通过邮政编码和公司名称进行搜索,这两者都存在于通过客户实体中的映射值连接的地址表中,defaultaddress
.
为此,我不得不在名为 findCustomerByPostcode
的函数中编写自定义查询。像这样:
$this->getEntityManager()
->createQuery('SELECT c FROM AppBundle:Oc49Customer c JOIN AppBundle:Oc49Address a WITH c.defaultaddress = a.id WHERE a.postcode LIKE :postcode')
->setParameter('postcode','%'.$postcode.'%')
->getResult();
但是,当我对邮政编码执行搜索时,我在浏览器中收到以下错误:
One of listeners must count and slice given target
这是指
Paginator.php
KNP 包中的文件。我已经更新到最新版本 2.5 但我似乎无法摆脱这个错误,对我来说它甚至没有意义。非常感谢任何帮助,因为我想不出另一种通过连接表中的值进行搜索的方法。
它从邮政编码搜索中返回结果,然后在 Controller 中:
$customers = $customer_repository->findCustomerByPostcode($filter_value);
$paginator = $this->get('knp_paginator');
$pagination = $paginator->paginate(
$customers,
$request->query->get('page', 1),
20
);
最佳答案
您可以将 queryBuilder 与左连接一起使用
public function findCustomerByPostcode($postcode) {
$query = $this->entityManager->createQueryBuilder();
$query
->select('c', 'a')
->from('AppBundle:Customer', 'c')
->leftJoin('c.address', 'a')
->setParameter('postcode', $postcode)
return $query->getQuery()->getResult();
}
关于symfony - 听众之一必须对给定的目标进行计数和切片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39769044/