Skip to content

Instantly share code, notes, and snippets.

@asagajda
Last active December 19, 2015 14:59
Show Gist options
  • Save asagajda/5973304 to your computer and use it in GitHub Desktop.
Save asagajda/5973304 to your computer and use it in GitHub Desktop.
Add this to repository
<?php
public function getFilteredCount(array $get)
{
/* DB table to use */
$tableObjectName = 'YourBundle:Entity';
$cb = $this->getEntityManager()
->getRepository($tableObjectName)
->createQueryBuilder($alias)
->select("count(a.id)");
/*
* Filtering
* NOTE this does not match the built-in DataTables filtering which does it
* word by word on any field. It's possible to do here, but concerned about efficiency
* on very large tables, and MySQL's regex functionality is very limited
*/
if ( isset($get['sSearch']) && $get['sSearch'] != '' ){
$aLike = array();
for ( $i=0 ; $i<count($aColumns) ; $i++ ){
if ( isset($get['bSearchable_'.$i]) && $get['bSearchable_'.$i] == "true" ){
$aLike[] = $cb->expr()->like($aColumns[$i], '\'%'. $get['sSearch'] .'%\'');
}
}
if(count($aLike) > 0) $cb->andWhere(new Expr\Orx($aLike));
else unset($aLike);
}
/*
* SQL queries
* Get data to display
*/
$query = $cb->getQuery();
$aResultTotal = $query->getResult();
return $aResultTotal[0][1];
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment