Update batch with CodeIgniter

后端 未结 2 862
旧时难觅i
旧时难觅i 2021-01-02 07:43

I\'m trying to do a small open CMS with CodeIgniter and I\'m now working on the categories system.

I\'m really stuck with that and after many tries and forum post I

相关标签:
2条回答
  • 2021-01-02 08:27

    try to use UPDATE_BATCH

    $this->db->update_batch();
    
    
    
    $data = array(
       array(
          'title' => 'My title' ,
          'name' => 'My Name 2' ,
          'date' => 'My date 2'
       ),
       array(
          'title' => 'Another title' ,
          'name' => 'Another Name 2' ,
          'date' => 'Another date 2'
       )
    );
    
    $this->db->update_batch('mytable', $data, 'title'); 
    

    The first parameter will contain the table name, the second is an associative array of values, the third parameter is the where key.

    hope this help........................

    UPDATE 
    
    // Produces: 
    // UPDATE `mytable` SET `name` = CASE
    // WHEN `title` = 'My title' THEN 'My Name 2'
    // WHEN `title` = 'Another title' THEN 'Another Name 2'
    // ELSE `name` END,
    // `date` = CASE 
    // WHEN `title` = 'My title' THEN 'My date 2'
    // WHEN `title` = 'Another title' THEN 'Another date 2'
    // ELSE `date` END
    // WHERE `title` IN ('My title','Another title')
    
    0 讨论(0)
  • 2021-01-02 08:49

    First you cannot update multiple rows in one query, so there's nothing like insert_batch() for updates - you have to do it in a loop for each row. Next you should use 3rd table to link post id with category id in a one to many relation using foreign keys (InnoDB, worse performance) OR at least use category_id field in TABLE#2 that links to the category id, so you don't need to update this table in case of changes in TABLE#1.

    0 讨论(0)
提交回复
热议问题