CodeIgniter Forums
View update_batch without executing - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Model-View-Controller (https://forum.codeigniter.com/forumdisplay.php?fid=10)
+--- Thread: View update_batch without executing (/showthread.php?tid=68962)



View update_batch without executing - bhogsett - 09-19-2017

I am using a db->update_batch and as I am debugging I would like to view what will be in the update without executing the update.

Can this be done?

Thanks.


RE: View update_batch without executing - skunkbad - 09-19-2017

Consider using a manual transaction. For example, consider the following table:

Code:
CREATE TABLE `myTable` (
 `id` int(10) NOT NULL,
 `title` varchar(255) NOT NULL,
 `name` varchar(255) NOT NULL,
 `date` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;


Now the following code:


PHP Code:
$this->db->insert('myTable', array(
 
   'title' => 'My title',
 
   'name' => 'My Name 1',
 
   'date' => '1972-12-12 00:00:00'
));

$this->db->insert('myTable', array(
 
   'title' => 'Another title',
 
   'name' => 'Another Name 1',
 
   'date' => '1972-12-12 00:00:00'
));

$data = array(
 
  array(
 
     'title' => 'My title' ,
 
     'name' => 'My Name 2' ,
 
     'date' => '2003-06-28 00:00:00'
 
  ),
 
  array(
 
     'title' => 'Another title' ,
 
     'name' => 'Another Name 2' ,
 
     'date' => '2003-06-28'
 
  )
);

$this->db->trans_begin();
$this->db->update_batch('myTable'$data'title');
echo 
$this->db->last_query();
$this->db->trans_rollback(); 


This will output:


Code:
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 '2003-06-28 00:00:00'
    WHEN `title` = 'Another title'
    THEN '2003-06-28'
    ELSE `date`
  END
WHERE `title` IN ('My title', 'Another title')


No updates were actually made to the database.