如何用activerecord批量插入数据
不知道怎么批量插入,即使在控制器里使用模型循环插入也是只有一条数据,其他的多条数据根本没有进入的数据表中,请问老师这是什么情况
ad控制器是
$model = new Ad();
$up = new UploadForm();
if(Yii::$app->request->isPost){
$post = Yii::$app->request->post();
$files = UploadedFile::getInstances($up,'imgpath');
foreach($files as $file){
$up->imgpath = $file;
$filepath = $up->upload();
$post['Ad']['imgpath'] = $filepath;
if($filepath){
if($model->add($post)){
echo $filepath.'<br />';
}else{
echo 'no ok';
}
}
}ad模型是
public function add($data){
$this->createuser = 'admin';
$this->createtime = time();
if($this->load($data) && $this->validate()){
return $this->save();
}
return false;
}uploadform模型是
public function upload()
{
if ($this->validate()) {
$filename = 'ad_'.date('YmdHisT').rand(10000,99999) . '.' . $this->imgpath->extension;
if($this->imgpath->saveAs('./upload/ad/' . $filename)){
return $filename;
}
return false;
} else {
return false;
}
}