笨:INSERT无周期的多个记录(CodeIgniter: INSERT multiple reco

2019-07-20 03:28发布

这是可能的CI中的Active Record使用多个插入记录不为,的foreach等?

我当前的代码:

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

Answer 1:

笨活动记录具有功能insert_batch我认为这是你需要什么

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

同时适用于笨3.x和笨2.2.6

链接已经更新

insert_batch()为笨3.X

insert_batch()为笨2.X



文章来源: CodeIgniter: INSERT multiple records without cycle