0
votes

I am new to DataMapper ORM in CodeIgniter, here is my question. According to the manual :

Be careful with this method. Without having limited it with where statements or similar methods it will modify every single row on the table!

Also, this method bypasses validation, and can also operate on in-table foreign keys, so please be aware of the risks.

If the update method bypasses validation, what is the way to update a record validating it before update? A sample code will be appreciated.

3

3 Answers

0
votes

DataMapper ORM can't validate because it can only do that on data that you have fetched first. Using UPDATE lets MySQL modify the fields without checking the eventual PHP validation functions.

The solution is first grabbing all the rows you need, and using the save() function. Then PHP-side validation will be done.

http://datamapper.wanwizard.eu/pages/save.html

I am a bit rusty at DataMapper ORM but here's an example that should explain what you should do:

$users = new User();
$users->where('age', 25)->get();
foreach($users as $user)
{
    $user->age = 26;
    $user->save();
}
0
votes

The update() method is nothing more then an alias for $this->db->update().

It only checks if an update timestamp needs to be added to the updated records, and makes sure that the fields passed exist in the table you're going to update.

for updating objects, use save(). It will automatically determine if an INSERT or an UPDATE is needed.

0
votes

if you try $obj->save(); and dynamic validation rules ( dependent on some $_POST variable ) and still have no validation ,

try use $obj->force_validation(); before $obj->save();