简体   繁体   English

Cakephp 3模型创建

[英]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. 在cakephp 2中,在循环更新表时,要重置表ID和数据,我正在使用create,然后进行保存。 http://book.cakephp.org/2.0/en/models/saving-your-data.html#model-create-array-data-array 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 ? Cakephp 3的新ORM中是否具有等效功能,还是新数据库系统会自动处理此功能?

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);

You can use the newEntities() method to create a list of entities from request data. 您可以使用newEntities()方法从请求数据创建实体列表。 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() . 文档显示了newEntities()的预期输入格式。

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

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