2013-02-18 11 views
21

for、foreachなどを使わずにCodeIgniter Active Recordで複数のINSERTレコードを使用できますか?CodeIgniter:サイクルのない複数のレコードを挿入

私の現在のコード:

foreach($tags as $tag) { 
    $tag = trim($tag); 
    $data = array(
     'topic_id' => $topic_id, 
     'user_id' => $user_id, 
     'text'  => $tag 
    ); 
    $this->db->insert('topic_tags', $data); 
} 

答えて

56

CodeIgniterのアクティブなレコードは、私はそれはあなたがCodeIgniterの3.xとCodeIgniterの2.2.6

の両方に

$data = array(
    array(
     'title' => 'My title' , 
     'name' => 'My Name' , 
     'date' => 'My date' 
    ), 
    array(
     'title' => 'Another title' , 
     'name' => 'Another Name' , 
     'date' => 'Another date' 
    ) 
); 

$this->db->insert_batch('mytable', $data); 

// Produces: INSERT INTO mytable (title, name, date) VALUES ('My title', 'My name', 'My date'), ('Another title', 'Another name', 'Another date') 

作品を必要とするものだと思う機能insert_batchを持っています

更新されたリンク

insert_batch() for Codeigniter 3.x

insert_batch() for Codeigniter 2.x

+0

同様版のためにこの仕事をしていますか?レコードが存在する場合は、それを更新します。存在しない場合は - >挿入します。もちろん、内側の配列にidを追加します。 –

関連する問題