Add page limit to Collection
I do not see any way to limit the number of object in a Collection. Collection::setPageSize() and Collection::setPageNumber() add a limit clause to the query.
This commit is contained in:
1 parent
6106066ef1
commit
10e3fec8a2
1 file changed
+44
@@ -74,6 +74,16 @@ class CollectionCore implements Iterator, ArrayAccess, Countable
|
|||||||
* @var int Total of elements for iteration
|
* @var int Total of elements for iteration
|
||||||
*/
|
*/
|
||||||
protected $total;
|
protected $total;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var int Page number
|
||||||
|
*/
|
||||||
|
protected $page_number = 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var int Size of a page
|
||||||
|
*/
|
||||||
|
protected $page_size = 10;
|
||||||
|
|
||||||
protected $fields = array();
|
protected $fields = array();
|
||||||
protected $alias = array();
|
protected $alias = array();
|
||||||
@@ -334,6 +344,11 @@ class CollectionCore implements Iterator, ArrayAccess, Countable
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// All limit clause
|
||||||
|
if ($this->page_size)
|
||||||
|
$this->query->limit($this->page_size, $this->page_number * $this->page_size);
|
||||||
|
|
||||||
|
|
||||||
// Shall we display query for debug ?
|
// Shall we display query for debug ?
|
||||||
if ($display_query)
|
if ($display_query)
|
||||||
@@ -659,6 +674,35 @@ class CollectionCore implements Iterator, ArrayAccess, Countable
|
|||||||
}
|
}
|
||||||
return $this->fields[$field];
|
return $this->fields[$field];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the page number
|
||||||
|
*
|
||||||
|
* @param int $page_number
|
||||||
|
* @return Collection
|
||||||
|
*/
|
||||||
|
public function setPageNumber($page_number)
|
||||||
|
{
|
||||||
|
$page_number = (int)$page_number;
|
||||||
|
if ($page_number > 0) {
|
||||||
|
$page_number--;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->page_number = $page_number;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the nuber of item per page
|
||||||
|
*
|
||||||
|
* @param int $page_size
|
||||||
|
* @return Collection
|
||||||
|
*/
|
||||||
|
public function setPageSize($page_size)
|
||||||
|
{
|
||||||
|
$this->page_size = (int)$page_size;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generate uniq alias from association name
|
* Generate uniq alias from association name
|
||||||
|
|||||||
Reference in new issue
Block a user