yii 2一括入庫

1183 ワード

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