jqgrid, Symfony2 – how we can process data from jqgrid searchbox

The jqgrid send filtered data in this format:

 ?_search=true&filters=&nd=1332250471208&page=1&rows=20&searchField=emailAddress&searchOper=eq&searchString=testmailaddress%40bibibi.com&sidx=id&sord=asc  

if we want to process we can use such method:

 $oQ0 = $repository->createQueryBuilder('p')  
->orderBy('p.fieldname', 'ASC']);
if ( ($tmp = $request->get('_search')) && ($tmp == "true") && $request->get('searchField') ) {
$searchField = $request->get('searchField');
$searchString = $request->get('searchString');
$searchOper = $request->get('searchOper');
//numeric fields
if ( is_numeric( $request->get( $searchString ) ))
{
switch($searchOper)
{
case 'eq': $oQ0->andWhere( $oQ0->expr()->eq("p.".$searchField, $searchString) ); break;
case 'ne': $oQ0->andWhere($oQ0->expr()->not($oQ0->expr()->eq("p.".$searchField, $searchString))); break;
case 'nu': $oQ0->andWhere($oQ0->expr()->isNull("p.".$searchField));break;
case 'nn': $oQ0->andWhere( $oQ0->expr()->not( $oQ0->expr()->isNull("p.".$searchField)));break;
}
}
//text fields
if ( !is_numeric( $request->get( $searchString ) ))
{
switch($searchOper)
{
case 'eq': $oQ0->andWhere($oQ0->expr()->eq("p.".$searchField, $oQ0->expr()->literal($searchString))); break;
case 'ne': $oQ0->andWhere($oQ0->expr()->not($oQ0->expr()->eq("p.".$searchField, $oQ0->expr()->literal($searchString)))); break;
case 'bw': $oQ0->andWhere($oQ0->expr()->like("p.".$searchField, $oQ0->expr()->literal("%".$searchString)));break;
case 'bn': $oQ0->andWhere($oQ0->expr()->not($oQ0->expr()->like("p.".$searchField, $oQ0->expr()->literal("%".$searchString))));break;
case 'ew': $oQ0->andWhere($oQ0->expr()->like("p.".$searchField, $oQ0->expr()->literal($searchString."%")));break;
case 'en': $oQ0->andWhere($oQ0->expr()->not($oQ0->expr()->like("p.".$searchField, $oQ0->expr()->literal($searchString."%"))));break;
case 'cn': $oQ0->andWhere($oQ0->expr()->like("p.".$searchField, $oQ0->expr()->literal("%".$searchString."%")));break;
case 'nc': $oQ0->andWhere($oQ0->expr()->not($oQ0->expr()->like("p.".$searchField, $oQ0->expr()->literal("%".$searchString."%"))));break;
case 'nu': $oQ0->andWhere($oQ0->expr()->isNull("p.".$searchField));break;
case 'nn': $oQ0->andWhere( $oQ0->expr()->not( $oQ0->expr()->isNull("p.".$searchField)));break;
}
}
}
$oQ0 = $oQ0->getQuery();
$oQ0->execute();

This is only an example, I used the Doctrine2 Expr* classes:

http://docs.doctrine-project.org/projects/doctrine-orm/en/2.0.x/reference/query-builder.html#expr-classes

This entry was posted in doctrine2, Expr* classes, Symfony2. jqgrid. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s