예제 #1
0
 /**
  * Search for object in database. It combines count() and all() functions.
  *
  * This function makes pagination easier.
  *
  * @param int   $page    Page number. Min: 1.
  * @param int   $nbItems Number of items. Min: 1.
  */
 public function paginate($page, $nbItems, $distinct = false)
 {
     $page = $page >= 1 ? $page : 1;
     $nbItems = $nbItems >= 1 ? $nbItems : 1;
     $q = $this->getQueryOrNewSelect();
     $root = $this->_root;
     $q->limit($nbItems, ($page - 1) * $nbItems);
     // prefer group by than distinct
     $distinct && $q->groupBy($root::table()->getPrimaryKey());
     $res['rows'] = $this->all();
     $q->remove(array('limit', 'offset', 'orderBy', 'distinct', 'groupBy'));
     $attr = $distinct ? DB::distinct($root::table()->getPrimaryKey()) : '*';
     $res['count'] = $q->count($attr);
     return $res;
 }
예제 #2
0
 public function testDistinctObject()
 {
     $b = new SelectBuilder();
     $b->root('Article');
     $b->count(DB::distinct(Article::table()->getPrimaryKey()));
     $b->select(['*'], false);
     $components = $b->build();
     $c = new BaseCompiler();
     $sql = $c->compileSelect($components);
     $expected = 'SELECT * ,COUNT(DISTINCT `id`) FROM `articles`';
     $this->assertEquals($expected, $sql);
 }