简体   繁体   English

在Cakephp中保存多次

[英]Save multiple times in Cakephp

I want to save 10 times in Cakephp using Save function with for loop but it doesn't seem to work. 我想在Cakephp中使用带有for循环的Save函数保存10次,但它似乎不起作用。 It only saves one data. 它只保存一个数据。

How can we make it so it saves multiple data at one call? 我们如何才能在一次通话中保存多个数据呢?

Below is my code... 以下是我的代码......

        for($i=0;$i<10;$i++){
        $unique_id = $this->_unique($userData['User']['id']); // Unique id with userid as initial prefix
        $this->data['Invite'] = array('invited_user' => $userData['User']['id'], 'unique_id' => $unique_id);
        $this->User->Invite->save($this->data['Invite']);
            }

You need to call Model::create( ) for each iteration of the loop. 您需要为循环的每次迭代调用Model :: create()。 IE: IE:

<?php
    for( $i=0; $i<10; $i++ ){
        $this->User->Invite->create( );
        $unique_id = $this->_unique($userData['User']['id']);
        $this->data['Invite'] = array('invited_user' => $userData['User']['id'], 'unique_id' => $unique_id);
        $this->User->Invite->save($this->data['Invite']);
        unset( $this->data[ 'Invite' ] );
    }
?>

The answer is in the following book, page 94: Source: Bari, Ahsanul. 答案在下面的书,第94页:来源:Bari,Ahsanul。 CakePHP Application Development (p. 94). CakePHP应用程序开发(p.94)。 Packt Publishing. Packt Publishing。 Kindle Edition. Kindle版。 This is the what you can find in the book, and the answer to this question: 这是你在书中可以找到的,以及这个问题的答案:

$this->Book->create();
$this->Book->save($this->data);

Note: It is important to call the model's create() method just ahead of calling the model's save() method if the intention is to create a new record. 注意:如果要创建新记录,则在调用模型的save()方法之前调用模型的create()方法非常重要。

In other words, you need to use create() before save() every time you use save(). 换句话说,每次使用save()时都需要在save()之前使用create()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM