php - Doctrine QueryBuilder Re-Use Parts -


i want count fields fits conditions , them page page doctrine query builder.

i'm generating query depends filter fields.

first section counting records can calculate pages.

$qb = $em->createquerybuilder();  $qb     ->select('count(m.id)')     ->from('csmediabundle:mediaitem', 'm')     ->where(         $qb->expr()->eq('m.media', $media->getid())     );  $filters = $request->request->get('filter');  if(!empty($filters['size'])) {     foreach($filters['size'] $key => $value) {         if(!empty($value)) {             $qb->andwhere(                 $qb->expr()->eq('m.'.$key, ':'.$key)             )->setparameter($key, $value);         }     } }  if(!empty($filters['sliders'])) {      $qb         ->leftjoin('cssliderbundle:slideritem', 's', 'on', 'm.id = s.media_id')         ->andwhere(             $qb->expr()->in('s.sliders', $filters['sliders'])         ); }  $media_count = $qb->getquery()->getsinglescalarresult(); 

second section getting records calculated page using same filters, changing select , final parts (getsinglescalarresult getresult)

i wonder if there way change select , result parts not use filters again , again...

yeah, that's functions for:

function filter($qb, $filters) {     if (!empty($filters['size'])) {         foreach($filters['size'] $key => $value) {             if (!empty($value)) {                 $qb->andwhere(                     $qb->expr()->eq('m.'.$key, ':'.$key)                 )->setparameter($key, $value);             }         }     }      if (!empty($filters['sliders'])) {         $qb             ->leftjoin('cssliderbundle:slideritem', 's', 'on', 'm.id = s.media_id')             ->andwhere(                 $qb->expr()->in('s.sliders', $filters['sliders'])             );     }      return $qb; }  $filters = $request->request->get('filter');  // count $qb = $em->createquerybuilder(); $qb     ->select('count(m.id)')     ->from('csmediabundle:mediaitem', 'm')     ->where(         $qb->expr()->eq('m.media', $media->getid())     );  $media_count = filter($qb, $filters)->getquery()->getsinglescalarresult();  // entities $qb = $em->createquerybuilder(); $qb     ->select('m')     ->from('csmediabundle:mediaitem', 'm')     ->where(         $qb->expr()->eq('m.media', $media->getid())     );  $media_entities = filter($qb, $filters)->getquery()->getresult(); 

another way clone query builder object:

$qb = $em->createquerybuilder(); $qb->from('csmediabundle:mediaitem', 'm')     ->where(         $qb->expr()->eq('m.media', $media->getid())     );  $filters = $request->request->get('filter'); if (!empty($filters['size'])) {     foreach($filters['size'] $key => $value) {         if (!empty($value)) {             $qb->andwhere(                 $qb->expr()->eq('m.'.$key, ':'.$key)             )->setparameter($key, $value);         }     } }  if (!empty($filters['sliders'])) {     $qb         ->leftjoin('cssliderbundle:slideritem', 's', 'on', 'm.id = s.media_id')         ->andwhere(             $qb->expr()->in('s.sliders', $filters['sliders'])         ); }  $qb2 = clone $qb;  $qb->select('count(m.id)') $media_count = $qb->getquery()->getsinglescalarresult();  $qb2->select('m') $media_entities = $qb2->getquery()->getresult(); 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -