Magic Data Modification
When using DB::DBAL’s built in methods for returning data (fetch, fetchOne, fetchAll, find, findBy…, findOneBy…) data is returned as a Data Object. The DB::DBAL Data Object allows for magic manipulation, meaning that you can change the value of the column on the fly, just as you would change the value of an object property:
1 2 3 4 5 |
// Get the Data Object
$r = DB::MySQL()->table('myTable')->find(1); // Change the Value
$r->name = 'new name'; |
While using magic data manipulation changes the value of the data locally, the column value will not be changed in the database until you run the save() method.
1 2 3 4 5 6 7 8 9 10 |
// Get the Data Object
$r = DB::MySQL()->table('myTable')->find(1); // Change the Value
$r->name = 'new name'; // Database column unchanged
// Update database column
$r->save(); |
Magic data manipulation can also be used on results containing more than one row by accessing the Data Object directly through it’s array key or by using a foreach statement to rotate through all the rows returned.