Cakephp,使用 find 检索 HABTM 模型的数据

发布于 2024-08-25 22:01:11 字数 392 浏览 5 评论 0原文

我是 cakephp 的新手,我正在尝试完成一些应该相对容易的事情。 我有 2 个模型项目 &类别通过 HABTM 关系绑定。

我正在尝试执行以下查询 ->查找属于某个类别的所有项目

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

但是,当我这样做时,它会生成 SQL 错误:

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

我做错了什么?

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 following 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

橙味迷妹 2024-09-01 22:01:11

据我所知,你可以这样得到你想要的:

/*in Project Controller file*/

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

如果正确设置你的HABTM关系,你可能会得到如下的东西:

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

这就是你想要的,不是吗?参见 在食谱中的 cakephp 中使用 HABTM。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文