• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
View update_batch without executing

#1
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.
Reply

#2
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.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
2 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2018 MyBB Group.