2011-12-01 76 views
0

我正在研究一个CakePHP应用程序,该应用程序是应用程序--如在申请大学的应用程序中。它由一系列存储在数据库表中的表单以及一些管理工具组成。CakePHP:查询缺少相关记录

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

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

这一切都是说我需要一种方法来过滤掉与applications关联的表格中没有记录的应用程序。简单的做法是在从数据库返回后将其过滤出来,实际上,这是我第一次尝试过。但是,我不只是查询,我正在使用paginator,因此在返回结果集后会过滤分页。我需要能够在$paginate配置数组的conditions参数中将其过滤掉。

下面是相关的控制器代码,包括当前的过滤机制,它的工作原理,但与分页食堂:

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配置数组中的条件参数,但我不知道如何查询缺席的记录。

在此先感谢您的帮助!

+0

也许某种形式的左外连接?我建议仅仅在SQL上工作 - 甚至将你的问题改为只包含SQL。一旦你有这个工作,它应该很容易移植到蛋糕。如果您遇到问题,请将其作为新问题发布。 – Dave

回答

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')); 
}