未知方法“saveAll”

时间:2017-06-01 15:43:24

标签: cakephp

几年后我刚刚完成了我的第一个测试项目......我很困惑。

public function add()
{
    $post = $this->Posts->newEntity();
    if ($this->request->is('post')) {
        $post = $this->Posts->patchEntity($post, $this->request->getData());
        if ($this->Posts->saveAll($post)) {
            $this->Flash->success(__('The post has been saved.'));

            return $this->redirect(['action' => 'index']);
        }
        $this->Flash->error(__('The post could not be saved. Please, try again.'));
    }
    $parentPosts = $this->Posts->ParentPosts->find('list', ['limit' => 200]);
    $this->set(compact('post', 'parentPosts'));
    $this->set('_serialize', ['post']);
}

我提交表格后,我

未知方法“saveAll”

BadMethodCallException

我所有改变的是

$this->Posts->save($post) 

(工作正常)到

$this->Posts->saveAll($post)

为什么这种方法在这里不为人知?它与saveAssociated等相同,谢谢!

1 个答案:

答案 0 :(得分:1)

从CakePHP版本3.2.8,您可以一次保存多个记录,但使用的功能是 $ this-> Posts-> saveMany($ post) 。您可以使用cakephp文档中提到的代码。

https://book.cakephp.org/3.0/en/orm/saving-data.html#saving-multiple-entities