脚本宝典收集整理的这篇文章主要介绍了cakephp – Cake 3:如何使用primaryKey设置将新实体添加到数据库?,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
$imported = 0; foreach ($data as $record) { $entITy = $table->findOrCreate([$table->PRimaryKey() => $record[$table->primaryKey()]]); $entity = $table->patchEntity($entity,$record); if ($table->save($entity)) { $imported++; } }
[ ['id' => 25,'title'=>'some title'],['id'=> 3,'title' => 'some other title'],['id' => 4356,'title' => 'another title'] ]
此外,通过查看您的代码,数据似乎是一种可以立即转换为实体的格式,因此您可能希望一次创建它们.
$entities = $table->newEntities($data,[ // don't forget to restrict assignment one way or // another when working with external input,for // example by using the `fieldList` option 'fieldList' => [ 'id','title' ] ]); // you may want to check the validation results here before saving foreach ($entities as $entity) { if ($table->save($entity,['checkExisting' => false])) { // ... } // ... }
也可以看看
> Saving Entities
> Converting Request Data
> Avoiding Property Mass Assignment Attacks
> Calidating Data Before Building Entities
以上是脚本宝典为你收集整理的cakephp – Cake 3:如何使用primaryKey设置将新实体添加到数据库?全部内容,希望文章能够帮你解决cakephp – Cake 3:如何使用primaryKey设置将新实体添加到数据库?所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。