更新 CakePHP 中的多个 HABTM 关系
在 Cake 说明书中,所有示例似乎都使用 save()
/saveAll()
一次更新单个记录的 HABTM 关系。但是,我想使用一次调用创建多对多关联。例如,我有 3 个音乐会活动,并且有 2 个传单。我希望这两张传单与每个音乐会活动相关联。
我有 2 个表:events
、flyers
和一个联接表:events_flyers
。所以我想我可以手动将记录添加到连接表中。但我想知道是否有某种方法可以做类似的事情:
$this->Event->updateAll(
$flyers,
array('festival_id' => $id)
)
或者 updateAll()
仅适用于常规字段?
In the Cake cookbook, all the examples seem to use save()
/saveAll()
to update HABTM relationships for a single record at a time. However, I'd like to create many to many associations using a single call. For example, I have 3 concert events, and I have 2 flyers. And I want those 2 flyers to be associated with each of the concert events.
I have 2 tables, events
, flyers
and a join table: events_flyers
. So I suppose I could just add the records to the join table manually. But I was wondering if there was some way I could do something similar to:
$this->Event->updateAll(
$flyers,
array('festival_id' => $id)
)
Or does updateAll()
only work on regular fields?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
此答案不使用
updateAll
,但我知道您可以使用save
创建 HABTM 关联,并在$this->data
中专门键入数据>:那么使用这个,您可以更进一步,将
saveAll
与多个事件记录一起使用吗?例如:这对你有用吗?
This answer doesn't use
updateAll
, but I know that you can create HABTM associations usingsave
and specially keyed data in$this->data
:So using this, could you take it a step further and use
saveAll
along with multiple Event records? For example:Does that work for you?