如何保存 HABTM 每一侧包含多个项目的数组
我想导入一堆书签,每个书签都有多个关键字。每个关键字也可以连接到多个书签。
数据以 JSON 形式出现,它被解析为 PHP,如下所示:
Array
(
[0] => Array
(
[title] => example1
[url] => http://example.org
[keywords] => Array
(
[0] => Hello
[1] => World
)
)
)
然后我将其转换为 CakePHP 样式数组:
Array
(
[0] => Array
(
[Bookmark] => Array
(
[title] => example1
[url] => http://example.org
)
[Keyword] => Array
(
[0] => Array
(
[title] => Hello
)
[1] => Array
(
[title] => World
)
)
)
)
如果我执行 $this->Bookmark->save($data)
它保存了书签,我可以从 $this->Bookmark->id
获取书签的 ID。但是 $this->Keyword->saveAll($data)
没有执行任何操作。
如何保存书签并将所有关键字连接到它,而不需要保存关键字两次?
I want to import a bunch of Bookmarks, which have multiple Keywords each. Each Keyword can be connected to multiple Bookmarks as well.
The data comes in as JSON, which is parsed to PHP like this:
Array
(
[0] => Array
(
[title] => example1
[url] => http://example.org
[keywords] => Array
(
[0] => Hello
[1] => World
)
)
)
I then convert this into a CakePHP style array:
Array
(
[0] => Array
(
[Bookmark] => Array
(
[title] => example1
[url] => http://example.org
)
[Keyword] => Array
(
[0] => Array
(
[title] => Hello
)
[1] => Array
(
[title] => World
)
)
)
)
If I do a $this->Bookmark->save($data)
it saves the Bookmark, and I can get the ID from the Bookmark from $this->Bookmark->id
. But $this->Keyword->saveAll($data)
does not do anything.
How can I save the Bookmark and connect all the keywords to it, without saving Keywords twice?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为了保存书签数据
关键字数据
为了保存更新的
,您应该像这样更改您的数组
For saving the Bookmark data
For saving the Keyword data
Updated
,You should change ur array like this