I need to fetch 5 distinct Date type values from database based on a string. When I run SQL query at phpmyadmin the results are correct:
SELECT DISTINCT `date` FROM `collection` WHERE `date` LIKE "%2015-%" ORDER BY `collection`.`date` DESC LIMIT 0,5
Results:
- 2015-12-31
- 2015-12-30
- 2015-12-29
- 2015-11-30
- 2015-11-28
But when I build the query with Doctrine it basically returns latest 5 dates. It looks like the "LIKE" statement was ignored. Here is the POST controller:
/**
* @Route("/collection/InputHint", name="collectionInputHint")
* @Method("POST")
*/
public function collectionInputHint(Request $request)
{
$string = $request->get('value');
$entity = $request->get('entity');
$entityColumn = $request->get('entity-column');
$entityType = $request->get('entity-type');
$result = array();
$em = $this->getDoctrine()->getManager();
$objects = $em
->getRepository('AppBundle:'.$entity)
->createQueryBuilder($entity)
->select($entity.'.'.$entityColumn)
->distinct($entity.'.'.$entityColumn)
->where($entity.'.'.$entityColumn.' LIKE :string')
->setParameter('string', '%'.$string.'%')
->orderBy($entity.'.'.$entityColumn, 'DESC')
->setMaxResults(5)
->getQuery()
->getResult();
foreach ($objects as $object) {
$value = ($entityType == 'date') ? $object[$entityColumn]->format("Y-m-d") : $object[$entityColumn];
array_push($result, (string)$value);
}
return new Response(json_encode($result));
}
and the doctrine results are:
- 2016-01-30
- 2016-01-29
- 2015-12-31
- 2015-12-30
- 2015-12-28
Notice first 2 results are less similar to the $string than the rest of results. Also if I change order to ASC there are 5 dates from 2013 so the order is not a problem here. Any ideas?
app_dev.phpto the URL and then click on thequerieslink on the bottom, then look at the database queries to see what was actually run?