代码如下 | 复制代码 |
publicfunctioninsertSeveral($table,$array_columns) { $sql='' $params=array(); $i= 0; foreach($array_columnsas$columns) { $names=array(); $placeholders=array(); foreach($columnsas$name=>$value) { if(!$i) { $names[] =$this->_connection->quoteColumnName($name); } if($valueinstanceofCDbExpression) { $placeholders[] =$value->expression; foreach($value->paramsas$n=>$v) $params[$n] =$v; }else{ $placeholders[] =':'.$name.$i; $params[':'.$name.$i] =$value; } } if(!$i) { $sql='INSERT INTO '.$this->_connection->quoteTableName($table) .' ('. implode(', ',$names) .') VALUES (' . implode(', ',$placeholders) .')' }else{ $sql.=',('. implode(', ',$placeholders) .')' } $i++; } return$this->setText($sql)->execute($params); } $rows=array( array('id'=> 1,'name'=>'John'), array('id'=> 2,'name'=>'Mark') ); $command= Yii::app()->db->createCommand(); $command->insertSeveral('users',$rows); |