CakePHP 中的 UpdateAll 和 HABTM
我有两个模型和一个连接表:User
、Post
和 posts_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
ASPostsUser
SETPostsUser
.is_active
= 1 WHEREPostsUser.
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
ASPostsUser
SETPostsUser
.is_active
= 1 WHEREPostsUser
.user_id
= 1 ANDUser
.is_active
= 1
As you can see the User table isn't getting joined in the query. Is there away around this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否显式定义了
PostsUser
模型,或者您只是使用隐式自动生成的模型?看来是后者,所以该模型可能不会有任何关联。 您可以在进行更新调用之前动态绑定它们,也可以显式创建模型文件并定义关联。
即
Post
和User
模型相互连接,但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
andUser
models are connected to each other, but thePostsUser
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 fromPostsUser
it has no defined associations.