I have multiple rows in database.
I need to collect all the rows based on certain condition and change the value of particular column by removing a word from the column's value.
How it could be done with with CakePHP 3?
I have multiple rows in database.
I need to collect all the rows based on certain condition and change the value of particular column by removing a word from the column's value.
How it could be done with with CakePHP 3?
Try the following:
$model->updateAll(
['description' => "REPLACE (description, 's', 'a')"], // fields
['is_disabled' => 1] //condition
);
This will generate the following sql:
UPDATE
mytable
SET
description = REPLACE (description, 's', 'a')
WHERE
is_disabled = 1
Note that this is replacing the matching string ('s') everywhere where it appears in the description field - even in the middle of a word. This often leads to wellknown clbuttic mistakes.
Add this on top of the controller "use Cake\Datasource\ConnectionManager;"
$this->loadModel('Rewards');
$connection = ConnectionManager::get('default');
$results = $connection->execute("UPDATE rewards SET description = REPLACE (description, 's', 'a') where is_disabled=1");
I have values in description fields 'wwss' now that fields word replace into 'wwaa' like that.
For more explanation refer this link
http://book.cakephp.org/3.0/en/orm/database-basics.html
How can I use mySQL replace() to replace strings in multiple records?
Please review,Thanks!