CakePHP消除了分页的空结果 [英] CakePHP eliminating null results from pagination

查看:94
本文介绍了CakePHP消除了分页的空结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经搜索了这个,但问题的一部分是,我不知道如何简洁地描述我的问题。我在这里有两个相关的模型:应用和缺席。应用属于缺席,缺少多个应用程序。这是应用程序的模式:

  CREATE TABLE应用程序(
id int unsigned AUTO_INCREMENT PRIMARY KEY,
user_id int unsigned,
absence_id int unsigned
);

这很简单。我想用特定用户的应用程序(我在Absences控制器中这样做)分页所有缺席。我可以使用此代码接近:



absences_controller.php:

  $ this-> paginate = array(
'contains'=> array(
'Application'=> array(
'conditions'=& 'Application.user_id'=> $ viewer_id,
//'NOT'=> array('Application.id'=> null),

),

);
$ this-> set('absences',$ this-> paginate());

这会返回我想要的缺勤,但它也会返回所有缺少与应用程序关联的缺省。有没有方法来做这个没有自定义查询?我想保留我的应用程序Cakey。您可以看到我试图在代码中删除注释掉的NOT行中的空应用程序结果。



谢谢!

解决方案

您正面临正常的可控制行为。



您正在执行的操作是检索全部;如果Application.user_id = $ viewer_id,则加入应用程序。



获取所需内容的最简单方法是从应用程序点检索您的数据

  > $ this-> set('applications',$ this-> paginate('Application'=> array('conditions'=> array('Application.user_id'=> $ viewer_id) 


I've googled around for this, but part of the problem is that I'm not really sure how to concisely describe my problem. I have two relevant models here: Application and Absence. Application belongsTo Absence, Absence hasMany Application. Here's the schema for Application:

CREATE TABLE applications (
    id int unsigned AUTO_INCREMENT PRIMARY KEY,
    user_id int unsigned,
    absence_id int unsigned
);

It's pretty simple. I want to paginate all Absences with an Application from a particular user (I'm doing this in the Absences controller). I can get close with this code:

absences_controller.php:

$this->paginate = array(
    'contain' => array(
        'Application' => array(
            'conditions' => array(
                'Application.user_id' => $viewer_id,
                //'NOT' => array('Application.id' => null),
            )
        ),
    )
);
$this->set('absences', $this->paginate());

This does return the Absences I want, but it also returns all Absences that have no Application associated with them. Is there a way to do this without custom queries? I'd like to keep my app Cakey. You can see one of my attempts to strip the null Application results in the commented-out 'NOT' line in the code.

Thanks!

解决方案

You are facing the normal Containable behavior.

What you are doing is retreiving all Absence, and join application if Application.user_id = $viewer_id. You are not returning only the absences related to your user.

The simplest way to get what you want is retreiving your data from the Application point of view and joining the Absence model.

In applications_controller.php

 $this->set('applications', $this->paginate('Application' => array('conditions' => array('Application.user_id' => $viewer_id))));

这篇关于CakePHP消除了分页的空结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆