多插入在kohana orm3 [英] multi insert in kohana orm3
问题描述
在我的应用程序中,我有一个循环执行约1000次,在其中创建对象并将其保存.这是我用数据填充数据库的应用程序的一部分.共同点是这样的:
In my application i have a loop that executes about 1000 times, inside it i'm creating object and saving it. This is the part of application where i populate my database with data. In common this looks like this:
foreach(...){
...
try{
$object = new Model_Whatever;
$object->whatever=$whatever;
$object->save();}
catch(Exception $e){
...}
}
}
这将产生1000个INSERT查询.是否有可能以某种方式使kohana生产多刀片.将其分成10个插入,每个插入100个数据集.可能吗,如果是,那是怎么做的?
This produces 1000 of INSERT queries. Is it possible to, in some way, made kohana produce multi inserts. Split this into 10 inserts with 100 data sets in each. Is it possible and if yes that what is the way doing so?
推荐答案
尽管Kohana ORM不支持多重插入,但是您仍然可以按以下方式使用查询生成器:
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();
}
- 您仍然需要对数据进行拆分,以使上面的内容不会尝试执行具有1000次插入的查询.
- $ data假定一个数组数组,其值与列的顺序相对应
感谢#kohana中的以赛亚
thanks Isaiah in #kohana
这篇关于多插入在kohana orm3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!