Refactoring Eloquent pagination.

This commit is contained in:
Taylor Otwell 2011-07-21 07:16:45 -07:00
parent b29ba692ce
commit f7eeb85ec6
1 changed files with 35 additions and 9 deletions

View File

@ -180,6 +180,31 @@ private function _first()
return (count($results = Eloquent\Hydrator::hydrate($this->take(1))) > 0) ? reset($results) : null; return (count($results = Eloquent\Hydrator::hydrate($this->take(1))) > 0) ? reset($results) : null;
} }
/**
* Get paginated model results.
*
* @param int $per_page
* @return Paginator
*/
private function _paginate($per_page = null)
{
$total = $this->query->count();
if (is_null($per_page))
{
if ( ! property_exists(get_class($this), 'per_page'))
{
throw new \Exception("The number of models to display per page has not been specified.");
}
$per_page = static::$per_page;
}
$page = \System\Paginator::page(ceil($total / $per_page));
return new \System\Paginator($this->for_page($page, $per_page)->get(), $total, $per_page);
}
/** /**
* Retrieve the query for a 1:1 relationship. * Retrieve the query for a 1:1 relationship.
* *
@ -190,6 +215,7 @@ private function _first()
public function has_one($model, $foreign_key = null) public function has_one($model, $foreign_key = null)
{ {
$this->relating = __FUNCTION__; $this->relating = __FUNCTION__;
return $this->has_one_or_many($model, $foreign_key); return $this->has_one_or_many($model, $foreign_key);
} }
@ -203,6 +229,7 @@ public function has_one($model, $foreign_key = null)
public function has_many($model, $foreign_key = null) public function has_many($model, $foreign_key = null)
{ {
$this->relating = __FUNCTION__; $this->relating = __FUNCTION__;
return $this->has_one_or_many($model, $foreign_key); return $this->has_one_or_many($model, $foreign_key);
} }
@ -417,14 +444,11 @@ public function __unset($key)
*/ */
public function __call($method, $parameters) public function __call($method, $parameters)
{ {
if ($method == 'get') if (in_array($method, array('get', 'first', 'paginate')))
{ {
return $this->_get(); $method = '_'.$method;
}
if ($method == 'first') return $this->$method();
{
return $this->_first();
} }
if (in_array($method, array('count', 'sum', 'min', 'max', 'avg'))) if (in_array($method, array('count', 'sum', 'min', 'max', 'avg')))
@ -446,14 +470,16 @@ public static function __callStatic($method, $parameters)
{ {
$model = static::make(get_called_class()); $model = static::make(get_called_class());
if ($method == 'get' or $method == 'all') if ($method == 'all')
{ {
return $model->_get(); return $model->_get();
} }
if ($method == 'first') if (in_array($method, array('get', 'first', 'paginate')))
{ {
return $model->_first(); $method = '_'.$method;
return $model->$method();
} }
if (in_array($method, array('count', 'sum', 'min', 'max', 'avg'))) if (in_array($method, array('count', 'sum', 'min', 'max', 'avg')))