CakePHP,使用条件查找为HABTM模型检索数据 [英] Cakephp, Retrieve Data for HABTM Models using conditional find
问题描述
我想从项目控制器执行搜索,可以执行以下操作:
查找所有DISTINCT Project.scedule WHERE Category.slug!='uncategorised'
续集专家
我设法做的是检索所有不属于Category的项目,但是我不确定如何重新搜索数组的结果为DISTINCT Project.schedule值(需填写表单下拉菜单) 解决方案
在回答这个问题之前,我再次建议你阅读 HABTM CAKEPHP烹饪书,那么你可以自己完成这样的工作。
$ this->项目 - > ; bindModel(array(
'hasOne'=> array(
'CategorysProject',
'FilterCategory'=>数组(
'className'=> 'Category',
'foreignKey'=>假,
'conditions'=> array('FilterCategory.id = CategorysProject.category_id')
))));
$ this-> Project-> find('all',array(
'fields'=> array(DISTINCT(Project.scedule)),
'conditions'=> ; array('FilterCategory.slug!='=>'uncategorised')
));
There are 2 Models: Project & Category that are bind with HABTM relationship. I would like to perform a search from projects controller that can do the following:
FIND all DISTINCT Project.scedule WHERE Category.slug != 'uncategorised'
Apologies for the syntax, I'm no sequel expert.
What I have managed to do is to retrieve all projects that do not belong to Category uncategorised into an array however I'm not sure as to how to search again the array result for DISTINCT Project.schedule values (needed to fill out a form drop down)
Before answer this question,again I suggest you to read the HABTM in cookbook of CAKEPHP carefully,then you can finish jobs like this yourself.
$this->Project->bindModel(array(
'hasOne' => array(
'CategorysProject',
'FilterCategory' => array(
'className' => 'Category',
'foreignKey' => false,
'conditions' => array('FilterCategory.id = CategorysProject.category_id')
))));
$this->Project->find('all', array(
'fields' => array(DISTINCT (Project.scedule)),
'conditions'=>array('FilterCategory.slug !='=>'uncategorised')
));
这篇关于CakePHP,使用条件查找为HABTM模型检索数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!