lin*_*ndy 6 cakephp save cakephp-1.3
我想为一个模型保存几条记录.saveAll()如果不是出现问题,这很容易做到:
我有一个通知表单,我从a <select>和两个字段中分别选择主题和内容的多个用户.现在,当我输出$this->data包含的内容时,我有:
Array([Notification] => Array
(
[user_id] => Array
(
[0] => 4
[1] => 6
)
[subject] => subject
[content] => the-content-here
)
)
Run Code Online (Sandbox Code Playgroud)
我读过Cake 1.3的书,为了保存模型的多个记录,你必须得到$this->data类似的东西:
Array([Article] => Array(
[0] => Array
(
[title] => title 1
)
[1] => Array
(
[title] => title 2
)
)
)
Run Code Online (Sandbox Code Playgroud)
那么,我如何"分享"所有选定用户的主题和内容?
这些值必须像这样重复
Array([Notification] => Array(
[0] => Array
(
[user_id] => 4
[subject] => subjects
[content] => content
)
[1] => Array
(
[user_id] => 6
[subject] => subject
[content] => contents
)
)
)
$this->Notification->saveAll($data['Notification']); // should work
Run Code Online (Sandbox Code Playgroud)
如果你没有传递任何列值,这个蛋糕就会忽略它
首先,这个数据库设计需要规范化.
在我看来,像一个Notification可以有许多User相关的东西.同时,一个User可以有很多Notifications.因此,
在视图中,您只需使用以下代码自动调出多选表单以获取用户ID:
echo $this->Form->input('User');
Run Code Online (Sandbox Code Playgroud)
发送到控制器的数据将采用以下形式:
Array(
[Notification] => Array
(
[subject] => subject
[content] => contentcontentcontentcontentcontentcontent
),
[User] => Array
(
[User] => Array
(
[0] => 4
[1] => 6
)
)
)
Run Code Online (Sandbox Code Playgroud)
现在,您所要做的就是调用saveAll()函数而不是save().
$this->Notification->saveAll($this->data);
Run Code Online (Sandbox Code Playgroud)
这就是Cake的做法!