2013-02-18 8 views
21

È possibile utilizzare più record INSERT in CodeIgniter Active Record senza for, foreach ed ecc.?CodeIgniter: INSERISCI più record senza ciclo

mio codice corrente:

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

risposta

56

Codeigniter record attivo ha una funzione insert_batch penso che è quello che ti serve

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

funziona sia per i Codeigniter 3.xe Codeigniter 2.2.6

LINK AGGIORNATI

insert_batch() for Codeigniter 3.x

insert_batch() for Codeigniter 2.x

+0

fa questo lavoro per l'edizione come bene? Se il record esiste -> aggiornalo. Se non esiste -> inseriscilo. Ovviamente aggiungerei l'id agli array interni. –