我的存储库中有一个自定义查询,可以让我接受zipcodes或citynames作为值。
这些值将提供给自动填充文本输入。
public function findByZipOrCity($cz)
{
$qb = $this->createQueryBuilder('z');
if (substr($cz, 0, 1) == "0") {
$cz = substr($cz, 1);
$qb
->select('z')
->where($qb->expr()->orX(
$qb->expr()->like('z.city', ':czRequest'),
$qb->expr()->like('z.code', ':czRequest')
))
->andWhere('z.code <= :smaller')
->setParameter('czRequest', $cz . '%')
->setParameter('smaller', 9999);
} else {
$qb
->select('z')
->where($qb->expr()->orX(
$qb->expr()->like('z.city', ':czRequest'),
$qb->expr()->like('z.code', ':czRequest')
))
->setParameter('czRequest', $cz . '%');
}
return $qb->getQuery()->getArrayResult();
}
我使用此ArrayResult()输出autosuggest plgin接受的JSON:
$zipcodes = $this->getDoctrine()->getManager()->getRepository("AppBundle:Zip")->findByZipOrCity($zip);
$response = new JsonResponse();
$codes = array();
foreach ($zipcodes as $zipcode) {
$codes[] = array(
'id' => $zipcode['id'],
'country' => $zipcode['country'],
'city' => $zipcode['city'],
'code' => sprintf('%05d', $zipcode['code']),
);
}
$response->setData($codes);
return $response;
因此查询'berli'给出了上述结果,autosuggest看起来像这样:
我必须更改哪些功能?我可以将查询分组(我需要一个有效的zip)或者之后只对数组做'某事'吗?
任何提示都非常赞赏!
答案 0 :(得分:0)
您可以更改数组,如下所示:
{{1}}