multi insert in kohana orm3

Deadly 提交于 2019-11-29 12:02:52

Whilst the Kohana ORM doesn't support multi inserts, you can still use the query builder as follows:

$query = DB::insert('tablename', array('column1', 'column2','column3'));
foreach ($data as $d) {
    $query->values($d);
}
try {
    $result = $query->execute();
} catch ( Database_Exception $e ) {   
        echo $e->getMessage();
}
  • you'll still need to split the data up so the above doesn't try to execute a query with 1000 inserts.
  • $data assumes an array of arrays with the values corresponding to the order of the columns

thanks Isaiah in #kohana

php work very slow when insert multi array very big (so that method ::values have array_merge) so more fast:

class Database_Query_Builder_Bath_Insert 
    extends Database_Query_Builder_Insert{

    public static function doExecute($table, $data) {
        $insertQuery = DB::insert($table, array_keys(current($data)));

        $insertQuery->_values = $data;

        $insertQuery->execute();
    }
}
call_user_func_array([$query, 'values'], $data);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!