我有一个数组$brands
作为存储库函数public function getBrandsByFilter($brands)
的参数。我从ajaxPOST
方法中拯救了这个数组,它看起来像:
$brands = ["brand"
[
"caporal" => "caporal"
"adidas" => "adidas"
]
]
我想将数组的每个值(caporal、adidas)作为存储库的WHERE
查询子句的参数传递,但我有一个例外:
An exception occurred while executing 'SELECT a0_.brand AS brand_0 FROM article a0_ WHERE a0_.brand IN (?, ?) GROUP BY a0_.brand' with params ["caporal", "adidas"]:
SQLSTATE[HY093]: Invalid parameter number: parameter was not defined
这是我的存储库ArticleRepository.php
:
public function getBrandsByFilter($brands)
{
$qb = $this->createQueryBuilder('a');
$qb
->select('a')
->andWhere('a.brand IN (:brandFilter)')
->setParameter('brandFilter', $brands, Connection::PARAM_STR_ARRAY);
return $qb->getQuery()->getResult();
}
更新:这个问题已经解决了,正如评论中所说,它是由错误的数组格式引起的。但另一方面,我有一个新的问题
在我的控制器中,我很好地检索了查询getBrandsByFilter()
的结果,但我无法在jsonResponse()
中向Ajax
发送它。
这是我的controller
代码:
/**
* @Route("/ajax/request", options={"expose"=true}, name="ajax_request")
* @Method({"POST"})
*/
public function ajaxRequestAction(Request $request)
{
if ($request->isMethod('post')) {
$brands = $request->request->get('brand');
$repository = $this->getDoctrine()->getRepository('ArticleBundle:Article');
/* I retrieve my query result */
$articles = $repository->getBrandsByFilter($brands);
/* And send it toward Ajax */
$response = new JsonResponse();
return $response->setData(array('data' => $articles));
}
}
这里是我的ajax
函数:
$('#submitFilter').click(function () {
$.ajax({
type: 'POST',
url: Routing.generate("ajax_request"),
data: { brand: selectedBrand },
dataType: 'json'
})
// HERE I WANT RETRIEVE MY JsonRESPONSE
.done(function( data ) {
console.log(data);
for ( var i = 0; i < data.length; i++ ) {
Object.keys(data[i]).forEach(function (key) {
var propertyData = data[i][key];
//console.log(key);
//
})
}
});
})
当我调试控制器中的$articles
变量时,我有一个对象数组,如下所示:
array:8 [▼
0 => Article {#612 ▼
-id: 203
-fosUserId: null
-name: "article 1"
-category: "sous-vêtements"
-brand: "caporal"
-model: "running"
-gender: "unisex"
1 => Article {#610 ▶}
2 => Article {#631 ▶}
3 => Article {#619 ▶}
4 => Article {#657 ▶}
5 => Article {#635 ▶}
6 => Article {#695 ▶}
7 => Article {#633 ▶}
]
但是当我在ajax中调试data
时,我有一个空对象数组:
data: Array(8)
0: {}
1: {}
2: {}
3: {}
4: {}
5: {}
6: {}
7: {}
我不知道为什么在JsonRESPONSE中发送的对象已填充的情况下检索一个EMPTY对象数组。谢谢你帮我理解。
$brands
数组的内容应该是这样的;
$brands = [
["caporal" => "caporal"], ["adidas" => "adidas"]
];
或者;
$brands = [
["caporal"], ["adidas"]
];
它会起作用,但我认为在这种情况下不应该使用->andWhere
、->where
。
我在另一个问题中找到了解决方案,不幸的是,我以前从未见过这个问题。
您必须将getResult
替换为getArrayResult
,才能获得格式良好的查询结果数组。查看问题的解决方案
public function getBrandsByFilter($brands)
{
$qb = $this->createQueryBuilder('a');
$qb
->select('a')
->andWhere('a.brand IN (:brandFilter)')
->setParameter('brandFilter', $brands, Connection::PARAM_STR_ARRAY);
/* Here replace getResult() by getArrayResult() */
return $qb->getQuery()->getArrayResult();
}
在控制器中:
/**
* @Route("/ajax/request", options={"expose"=true}, name="ajax_request")
* @Method({"POST"})
*/
public function ajaxRequestAction(Request $request)
{
if ($request->isMethod('post')) {
$brands = $request->request->get('brand');
$repository = $this->getDoctrine()->getRepository('ArticleBundle:Article');
$articles = $repository->getBrandsByFilter($brands);
return new JsonResponse($articles);
}
}