2013-02-18 18 views
21

Możliwe jest użycie wielu rekordów INSERT w aktywnym rekordzie CodeIgniter bez for, foreach i innych?CodeIgniter: INSERT wiele rekordów bez cyklu

Mój bieżący kod:

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); 
} 

Odpowiedz

56

CodeIgniter aktywny rekord posiada funkcję insert_batch myślę, że to, co trzeba

$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') 

działa zarówno CodeIgniter 3.xi CodeIgniter 2.2.6

ZAKTUALIZOWANE LINKI

insert_batch() for Codeigniter 3.x

insert_batch() for Codeigniter 2.x

+0

to działa do wersji, jak również? Jeśli rekord istnieje -> zaktualizuj go. Jeśli nie istnieje -> włóż to. Oczywiście powinienem dodać identyfikator do wewnętrznych tablic. –

Powiązane problemy