建议多插入Cakephp 1模型

时间:2012-05-24 22:24:51

标签: cakephp

我想知道是否有更多“cakephp like”的方式将user_id插入到同一个表中的多个插入中。我做的例子:

$user_id = $this->Auth->user('id');
if ($this->request->is('post')) {
        foreach ($this->request->data['Post'] as $key => $value) {
            if (is_int($key)) {
                $this->request->data['Post'][$key]['user_id'] = $user_id;
            }
            unset($key);
            unset($value);
        }
        debug($this->data['Post']);
        die();
        $this->Post->saveAll($this->data['Post']);
    }

我不使用输入隐藏值user_id来表示原因。 谢谢你的帮助 !

2 个答案:

答案 0 :(得分:0)

如果我的记忆回忆正确,您的数据需要如下所示,并在用户模型上调用saveAll

Array
(
    [User] => Array
        (
            [id] => {you_user_id}
        )
    [Post] => Array
        (
            [0] => Array
                (
                    [field] => blah
                )
            [1] => Array
                (
                    [field] => blah
                )
        )
)

换句话说:

$this->request->data['User'] = array('id' => $this->Auth->user('id'));
$this->Post->User->saveAll($this->request->data);

答案 1 :(得分:0)

尝试这样的事情:

$result = Set::insert($this->request->data, 'Post.{n}.user_id', array('user_id' => $user_id));