CakePHP:使用自定义数组填充paginate()
我想为CakePHP的CakePHP:使用自定义数组填充paginate(),cakephp,pagination,Cakephp,Pagination,我想为CakePHP的paginate()函数提供我自己构建的数组,如下所示: class SomeController extends AppController { var $paginate = array( 'limit' => 25, 'order' => array( 'Post.title' => 'asc' ) ); public function i
paginate()
函数提供我自己构建的数组,如下所示:
class SomeController extends AppController {
var $paginate = array(
'limit' => 25,
'order' => array(
'Post.title' => 'asc'
)
);
public function index(){
$theArray = array(
array(
'Entity1'=>
array('id'=>1, 'someField'=>'value1'),
'SecondEntity'=>
array('id'=>1, 'fieldTwo'=>'reallyInteresting')
),
array(
'Entity1'=>
array('id'=>2, 'someField'=>'value2'),
'SecondEntity'=>
array('id'=>2, 'fieldTwo'=>'displayedValue')
)
);
$this->set('data',$this->paginate($theArray));
}
}
我怎样做才是最简单的方法
public function paginate($array, $page = 0) {
// array_slice() to extract needed portion of data (page)
// usort() to sort data using comparision function $this->sort()
return(
array_slice(
usort(
$array,
array($this, 'sort') // callback to $this->sort()
),
$page*$this->paginate['limit'],
$this->paginate['limit']
)
);
}
public function sort($a, $b) {
$result = 0;
foreach($this->paginate['order'] as $key => $order) {
list($table, $field) = explode('.', $key);
if($a[$table][$field] == $b[$table][$field])
continue;
$result = ($a[$table][$field] < $b[$table][$field] ? -1 : 1) *
($order == 'asc' ? 1 : -1);
}
return($result);
}
公共函数分页($array,$page=0){
//用于提取所需数据部分的数组_slice()(第页)
//usort()使用比较函数$this->sort()对数据进行排序
返回(
数组块(
乌斯波特(
$array,
数组($this,'sort')//回调到$this->sort()
),
$page*$this->paginate['limit'],
$this->paginate['limit']
)
);
}
公共函数排序($a,$b){
$result=0;
foreach($this->paginate['order']为$key=>$order){
列表($table,$field)=分解('.',$key);
如果($a[$table][$field]==$b[$table][$field])
继续;
$result=($a[$table][$field]<$b[$table][$field]?-1:1)*
($order=='asc'-1:-1);
}
返回($结果);
}