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:
Maxence
2013-06-02 21:04:48 +03:00
parent 6106066ef1
commit 10e3fec8a2
+44
View File
@@ -74,6 +74,16 @@ class CollectionCore implements Iterator, ArrayAccess, Countable
* @var int Total of elements for iteration
*/
protected $total;
/**
* @var int Page number
*/
protected $page_number = 0;
/**
* @var int Size of a page
*/
protected $page_size = 10;
protected $fields = array();
protected $alias = array();
@@ -334,6 +344,11 @@ class CollectionCore implements Iterator, ArrayAccess, Countable
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 ?
if ($display_query)
@@ -659,6 +674,35 @@ class CollectionCore implements Iterator, ArrayAccess, Countable
}
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