Table of Contents
yii批量插入数据
您可能感兴趣的文章
Home php教程 php手册 yii批量插入数据

yii批量插入数据

Jun 13, 2016 am 09:00 AM
array name user yii batch insert data have Now

yii批量插入数据

现在有如下数据:

$user=array(
	0=>array('id'=>1,'name'=>'张三'),
	0=>array('id'=>2,'name'=>'李四'),
);
Copy after login

现在如果需将这两条数据批量插入到数据表的话,一般大家都会想到用foreach,然后调用 insert 或者 save 方法插入数据。但实际操作却是在foreach循环操作中,insert 只会插入第一条数据,save 却只会插入第二条数据。这是为什么呢?解释如下:

插入一行到基于这个ActiveRecord的属性表。如果表的主键是自动增量和插入前为null,将插入后的实际值填充。注意,验证不执行此方法。你可以调用validate执行验证。记录成功插入到数据库后,它的isNewRecord属性将设置为false,和它的scenario属性将设置为 update。

既然如此,那么除了使用事务该如何实现数据的批量插入呢?经试验总结了如下两种方法:

第一种方法

$model=new User();
foreach($data as $attributes){
     $_model=clone $model;
     $_model->setAttributes($attributes);
     $_model->save();
}
Copy after login

第二种方法

$model=new User();
foreach($data as $attributes){
      $model->isNewRecord=true;
      $model->setAttributes($attributes);
      $model->save()&&$model->id=0;
}
Copy after login

您可能感兴趣的文章

  • Yii framework框架之模块开发分析
  • Yii框架Yiiapp()的理解
  • yii 数据库添加,修改,删除相关操作总结
  • yii框架如何配置默认controller与action
  • Yii中的数据库事务的使用方法小结
  • Yii控制器动作参数绑定处理
  • yii数据库查询操作总结
  • yii框架缓存知识总结
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Use ddrescue to recover data on Linux Use ddrescue to recover data on Linux Mar 20, 2024 pm 01:37 PM

Use ddrescue to recover data on Linux

Open source! Beyond ZoeDepth! DepthFM: Fast and accurate monocular depth estimation! Open source! Beyond ZoeDepth! DepthFM: Fast and accurate monocular depth estimation! Apr 03, 2024 pm 12:04 PM

Open source! Beyond ZoeDepth! DepthFM: Fast and accurate monocular depth estimation!

How to use Excel filter function with multiple conditions How to use Excel filter function with multiple conditions Feb 26, 2024 am 10:19 AM

How to use Excel filter function with multiple conditions

The vitality of super intelligence awakens! But with the arrival of self-updating AI, mothers no longer have to worry about data bottlenecks The vitality of super intelligence awakens! But with the arrival of self-updating AI, mothers no longer have to worry about data bottlenecks Apr 29, 2024 pm 06:55 PM

The vitality of super intelligence awakens! But with the arrival of self-updating AI, mothers no longer have to worry about data bottlenecks

Google is ecstatic: JAX performance surpasses Pytorch and TensorFlow! It may become the fastest choice for GPU inference training Google is ecstatic: JAX performance surpasses Pytorch and TensorFlow! It may become the fastest choice for GPU inference training Apr 01, 2024 pm 07:46 PM

Google is ecstatic: JAX performance surpasses Pytorch and TensorFlow! It may become the fastest choice for GPU inference training

Slow Cellular Data Internet Speeds on iPhone: Fixes Slow Cellular Data Internet Speeds on iPhone: Fixes May 03, 2024 pm 09:01 PM

Slow Cellular Data Internet Speeds on iPhone: Fixes

The U.S. Air Force showcases its first AI fighter jet with high profile! The minister personally conducted the test drive without interfering during the whole process, and 100,000 lines of code were tested for 21 times. The U.S. Air Force showcases its first AI fighter jet with high profile! The minister personally conducted the test drive without interfering during the whole process, and 100,000 lines of code were tested for 21 times. May 07, 2024 pm 05:00 PM

The U.S. Air Force showcases its first AI fighter jet with high profile! The minister personally conducted the test drive without interfering during the whole process, and 100,000 lines of code were tested for 21 times.

The first robot to autonomously complete human tasks appears, with five fingers that are flexible and fast, and large models support virtual space training The first robot to autonomously complete human tasks appears, with five fingers that are flexible and fast, and large models support virtual space training Mar 11, 2024 pm 12:10 PM

The first robot to autonomously complete human tasks appears, with five fingers that are flexible and fast, and large models support virtual space training

See all articles