yii2批量入库

来源:互联网 发布:网络精灵怎么关闭 编辑:程序博客网 时间:2024/05/22 08:21
<span style="font-size:18px;">/**action 批量入库*$arr  必须是二维数组**/function many_file($file)    {        //echo count($file);die;        $arr = array();        foreach($file as $k=>$v)        {            $img = $v->name;            $images = 'uploads/'.$img;            $arr[]['images'] = $images;        }        $connection = \Yii::$app->db;          //数据批量入库          return $connection->createCommand()->batchInsert(              'manyfile',              ['images'],            $arr         )->execute();      }</span>



或者

<span style="font-size:18px;">$model = new User(); foreach($data as $attributes)  { $_model = clone $model;   $_model->setAttributes($attributes);   $_model->save();  }</span>


还可以

<span style="font-size:18px;">$model = new User();foreach($data as $attributes)  {$model->isNewRecord = true;  $model->setAttributes($attributes); $model->save() && $model->id=0;  }</span>


0 0
原创粉丝点击