CakePHP:查询缺少相关记录

时间:2011-12-01 22:41:44

标签: cakephp

我正在开发一个CakePHP应用程序,它是一个应用程序 - 就像一个申请大学的应用程序一样。它由一系列表单和一些管理工具组成,每个表单都存储在数据库表中。

将所有表绑定在一起是一个应用程序表,它将元数据存储在应用程序中。构成预期学生应用程序的所有各种表都属于应用程序表 - 换句话说,它们共享一对一的关系。

该应用程序允许人们在完成它的过程中走开,然后再回来。结果,管理界面变得混乱,应用程序不完整。接口的用户希望仅查看已完成到某一点的不完整应用程序。他们想要的信息来自某个表格中的必填表格字段,因此如果某人完成了该步骤,则会出现所需的信息。

这就是说我需要一种方法来过滤掉与applications相关联的一个表中缺少记录的应用程序。执行此操作的简单方法是在从数据库返回后将其过滤掉,实际上,这是我第一次尝试的。但是,我不只是查询,我正在使用分页器,因此在返回结果集之后对结果集进行过滤会使分页变得混乱。我需要能够在conditions配置数组的$paginate参数中对其进行过滤。

这是相关的控制器代码,包括当前的过滤机制,它有效,但与分页混淆:

var $paginate = array(
        'limit'=>100,
        // We want apps that are complete at the top of the list, ordered by the date they were finished.
        // We want to order incomplete apps by dateStarted because they don't have a dateFinished yet.
        'order'=>array('stepsCompleted'=>'desc', 'dateFinished'=>'desc', 'dateStarted'=>'desc'),
    );

function admin_index() {
    $this->Nsapp->recursive = 0;
    $this->Nsapp->unbindModel(array('hasOne'=>array('Essay', 'Religion', 'SpecialNeed')), false);
    $nsapps = $this->paginate('Nsapp');
    // Filter out the apps that are too incomplete to do anything about
    foreach ($nsapps as $key => $nsapp)  {
        // This fairly horrible conditional says, "If they provided us with at least one name and an email, do not filter them out"
        if ((empty($nsapp['Person']['name1']) &&
             empty($nsapp['Person']['name2']) &&
             empty($nsapp['Person']['surName'])) ||
            empty($nsapp['Person']['email']))
        {
            unset($nsapps[$key]);
        }
    }

    $this->set('nsapps', $nsapps);
}

因此,总结和重述:我当前的分页查询返回没有记录的关联表的空字段数组。我需要一种方法来删除缺少某个相关记录的结果,而不是通过后过滤(因为这与分页混淆),但是使用$paginate配置数组中的conditions参数,但我不知道如何查询记录的缺席

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

尝试在执行数据库查询时设置过滤器。

function admin_index() {
    $this->Nsapp->recursive = 0;
    $this->Nsapp->unbindModel(array('hasOne'=>array('Essay', 'Religion', 'SpecialNeed')), false);
    $nsapps = $this->paginate('Nsapp');
    $this->paginate['Nsapp']['conditions'] = 
          array("not"=>array("Person.name1"=>null, "Person.name2"=>null));
    pr($this->paginate('Nsapp'));

    $this->set('nsapps', $this->paginate('Nsapp'));
}
相关问题