0

我想知道是否有另一种更“像 cakephp 一样”的方式将 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。谢谢你的帮助 !

4

2 回答 2

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);
于 2012-05-24T22:33:45.883 回答
0

尝试这样的事情:

$result = Set::insert($this->request->data, 'Post.{n}.user_id', array('user_id' => $user_id));
于 2012-05-24T22:37:41.020 回答