YII框架批量插入数据的方法

public function insertSeveral($table, $array_columns) { $sql = ''; $params = array(); $i = 0; foreach ($array_columns as $columns) { $names = array(); $placeholders = array(); foreach ($columns as $name => $value) { if (!$i) { $names[] = $this->_connection->quoteColumnName($name); } if ($value instanceof CDbExpression) { $placeholders[] = $value->expression; foreach ($value->params as $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);

更多关于Yii相关内容感兴趣的读者可查看本站专题:《Yii框架入门及常用技巧总结》、《php优秀开发框架总结》、《smarty模板入门基础教程》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/8445ba589f6c3ba51ba58d8120623e18.html