trante
trante

Reputation: 33996

Cakephp 3 model create

In cakephp 2, while updating a table in a loop, to reset table id and data, I was using create and then making a save. http://book.cakephp.org/2.0/en/models/saving-your-data.html#model-create-array-data-array

$data = array('username' => usernameArray[$i], 'age' => $ageArray[$i] );
$this->Mytable->create();
$this->Mytable->save($data);

Is there an equivalent function in Cakephp 3's new ORM, or does new database system handles this automatically ?

To use remaining code I will use this code in a loop. Do I need a create function ?

$data = array('username' => usernameArray[$i], 'age' => $ageArray[$i] );
$tableInstance = TableRegistry::get('Mytable');
$entity = $tableInstance->newEntity($data);
$tableInstance->save($entity);

Upvotes: 0

Views: 3004

Answers (1)

Mark Story
Mark Story

Reputation: 1269

You can use the newEntities() method to create a list of entities from request data. You can then use a loop to save the entities.

$this->loadModel('Users');
$users = $this->Users->newEntities($this->request->data());
foreach ($users as $user) {
   $this->Users->save($user);
}

The docs show the expected input format for newEntities().

Upvotes: 3

Related Questions