cakephp分页和精简控制器
我正在使用cakephp(2.4.7)进行开发,在组织控制器和模型以使用分页时遇到问题 到目前为止,我在模型中使用了最多的逻辑(瘦控制器、大模型)。在那里,我将结果返回给控制器,在那里我设置变量以在视图上显示它 但现在我想使用分页。这打破了我的概念,因为我不能在模型内部使用分页 解决这个问题的最好办法是什么?我不想重新调整我的整个结构,因为我需要在许多不同的动作和模型中进行分页 例如: 控制器用户、行动朋友cakephp分页和精简控制器,php,cakephp,pagination,cakephp-2.0,Php,Cakephp,Pagination,Cakephp 2.0,我正在使用cakephp(2.4.7)进行开发,在组织控制器和模型以使用分页时遇到问题 到目前为止,我在模型中使用了最多的逻辑(瘦控制器、大模型)。在那里,我将结果返回给控制器,在那里我设置变量以在视图上显示它 但现在我想使用分页。这打破了我的概念,因为我不能在模型内部使用分页 解决这个问题的最好办法是什么?我不想重新调整我的整个结构,因为我需要在许多不同的动作和模型中进行分页 例如: 控制器用户、行动朋友 public function friends($userid = null, $slu
public function friends($userid = null, $slug = null) {
$this->layout = 'userprofile';
$this->User->id = $userid;
if (!$this->User->exists()) {
throw new NotFoundException(__('Invalid User'));
}
$this->set('friends', $this->User->getFriendsFrom($userid));
}
用户模型,函数getFriendsFrom($User\u from)。。我在不同的行动中需要这种方法
public function getFriendsFrom($user_from) {
$idToFind = $user_from;
$data = $this->FriendFrom->find('all',
array(
'conditions'=>array(
'OR'=> array(
array('user_to'=> $idToFind),
array('user_from'=> $idToFind)
),
'AND' => array(
'friendship_status' => 1
)
),
'contain' => array('UserFrom.Picture', 'UserTo.Picture')
)
);
$friendslist = array();
foreach ($data as $i) {
if ($i['FriendFrom']['user_from'] == $idToFind){
$friendslist[] = $i['UserTo'];
}
elseif ($i['FriendFrom']['user_to'] == $idToFind){
$friendslist[] = $i['UserFrom'];
}
}
return $friendslist;
}
使用分页设计这个概念的最佳方法是什么
感谢控制器中的用户使用cakephp Paginator
var $helpers = array('Paginator');
现在调用以下方法
function index() {
$result = array(
'recursive' => -1,
'conditions' => array(...),
'contain' => array(...),
'limit' => '2'
);
// you can write the above code in your model
$this->paginate = $result;
$users = $this->paginate('User');
// Re-arrage $users
$this->set(compact('users'));
}
如果有任何问题,请告诉我 现在,在$friends中,所有用户都具有所有其他信息(关联表)。但是getFriendsFrom()的结果并不能解释表的结构。我认为您的getFriendsFrom()会根据您的查询返回行。在friends操作中,users表与许多其他表(Gallery等)关联。我只想显示朋友。但是在您的解决方案中,$friends变量将填充所有注册用户。我想是因为$this->paginate('User');我建议您首先编写查询并调试查询,如果查询从表中返回所需的记录,则应用cakephp Paginator的分页帮助。请看下面的链接@Supravat,我也有同样的问题。我认为getFriendsFrom方法正确地返回了数据。但是$friends=$this->paginate('User')$此->设置('friends',$friends);等于$friends=$this->User->find('all')$此->设置('friends',$friends);这是错误的,因为$friends并没有填充来自getFriendsFrom的结果。。