Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
435 views
in Technique[技术] by (71.8m points)

multi insert in kohana orm3

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){
    ...}
}
                    }

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...