I have an array $brands
as parameter of my repository function public function getBrandsByFilter($brands)
. I rescue this array from an ajax POST
method and it looks like at :
$brands = ["brand"
[
"caporal" => "caporal"
"adidas" => "adidas"
]
]
I'd like to pass each values (caporal, adidas) of my array as arguments of my WHERE
query clause of my repository but I have this exception :
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
Here is my repository 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();
}
In my controller I retrieve well the result of my query getBrandsByFilter()
but I don't get to send it in a jsonResponse()
toward Ajax
.
Here is my controller
code :
/**
* @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));
}
}
And here my ajax
function :
$('#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);
//
})
}
});
})
When I debug the $articles
variable in my controller, I have an array of objects like this :
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 ▶}
]
But when I debug data
in my ajax I have an array of empty objects :
data: Array(8)
0: {}
1: {}
2: {}
3: {}
4: {}
5: {}
6: {}
7: {}
I don't know why I retrieve an array of EMPTY objects while the one that I send in the JsonRESPONSE is filled. Thank you for helping me understand.
I find the solution in an other issue wich unfortunately I have not seen before.
You have to replace getResult
by getArrayResult
to get an array well formatted of the query result. See the solution of the issue
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();
}
And in teh 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');
$articles = $repository->getBrandsByFilter($brands);
return new JsonResponse($articles);
}
}