Cakephp, Retrieve Data for HABTM Models using conditional find

走远了吗. 提交于 2019-12-24 19:16:20

问题


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)


回答1:


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


来源:https://stackoverflow.com/questions/2488524/cakephp-retrieve-data-for-habtm-models-using-conditional-find

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!