CakePHP 中的 UpdateAll 和 HABTM

发布于 2024-07-30 08:22:45 字数 805 浏览 2 评论 0原文

我有两个模型和一个连接表:UserPostposts_users

posts_users 表包含一些额外字段,其中之一是 is_active。 我想将所有用户帖子设置为活动状态,前提是用户本身处于活动状态。

我有以下

$this->Post->PostsUser->updateAll(array('PostsUser.is_active' => 1), array('PostsUser.user_id' => $data['User']['id'], 'User.is_active' => 1));

导致 MySQL 错误的情况

警告(512):SQL 错误:1054:“where 子句”中的未知列“User.is_active”...

查询: UPDATE posts_sites AS PostsUser SET PostsUser.is_active = 1 WHERE PostsUser.user_id = 1 和User.is_active = 1

如您所见,用户表没有加入查询中。 有办法解决这个问题吗?

I have two models and a join table, User, Post and posts_users.

The posts_users table contains a few extra fields, one of which is is_active. I want to set all of a users posts to active providing the user is active themselves.

I have the following

$this->Post->PostsUser->updateAll(array('PostsUser.is_active' => 1), array('PostsUser.user_id' => $data['User']['id'], 'User.is_active' => 1));

Which causes a MySQL error

Warning (512): SQL Error: 1054: Unknown column 'User.is_active' in 'where clause' ...

Query: UPDATE posts_sites AS PostsUser SET PostsUser.is_active = 1 WHERE PostsUser.user_id = 1 AND User.is_active = 1

As you can see the User table isn't getting joined in the query. Is there away around this problem?

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

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

发布评论

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

评论(1

如歌彻婉言 2024-08-06 08:22:45

您是否显式定义了 PostsUser 模型,或者您只是使用隐式自动生成的模型?

看来是后者,所以该模型可能不会有任何关联。 您可以在进行更新调用之前动态绑定它们,也可以显式创建模型文件并定义关联。

PostUser 模型相互连接,但 PostsUser 模型只是一个空的辅助模型,悬挂在它们中的任何一个上。 它本身没有任何连接,因此当从 PostsUser 进行调用时,它没有定义的关联。

  Post   <--->   User
    |             |
    v             v
PostsUser     PostsUser

Have you defined the PostsUser model explicitly, or are you just using the implicitly auto-generated model?

It seems the latter is the case, so that model will probably not have any associations. You could bind them on the fly before making the update call, or you could explicitly create the model file and define the associations.

I.e. the Post and User models are connected to each other, but the PostsUser model is just an empty helper model that dangles off of either of them. It does not by itself have any connections, so when making a call from PostsUser it has no defined associations.

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