开发者

Cakephp, Retrieve Data for HABTM Models using find

I am new to cakephp and I'm trying to accomplish something that should be relatively easy. I have 2 models projects & categories bind by HABTM relationship.

I am trying to perform the followin开发者_运维百科g query -> find all projects that belong to a category

$projects = $this->Project->find('all', array('conditions' => array('Category.slug' => $category)));

However when I'm doing so it generates an SQL error:

SQL Error: 1054: Unknown column 'Category.slug' in 'where clause' 

What am I doing wrong?


As far as I know,you can get what you want like this:

/*in Project Controller file*/

$categorys = $this->Project->Category->find('all', array('conditions' => array('Category.slug' => $category)));

And you will probably get somethin as follows if set you HABTM relationship correctly:

Array
(  
    [Category] => Array
    (
        [id] => xxx
        [name] => hello there
        ...
    )

    [Project] => Array
    (
        [0] => Array
            (
                [id] => 123
                [name] => Breakfast
            )
       [1] => Array
            (
                [id] => 124
                [name] => Dessert
            )
       [2] => Array
            (
                [id] => 125
                [name] => Heart Disease
            )
    )
)

It's what's you want,isn't it?See work with HABTM in cakephp in the cookbook.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜