WanWizard / Datamapper

Maintenance of Datamapper has been transferred to https://github.com/saekort/datamapper in 2012.
27 stars 3 forks source link

Run Validation also for update() #11

Open WanWizard opened 12 years ago

WanWizard commented 12 years ago

Requested by @ingdiaz

Sorry my bad English.

I would like add method for custom querys or calls to views and other method as ExecuteScalar return only a column with a row, methods for Store Procedures, other thing when I use the update method no use validate like the save() method, this would be very useful.

Thankyou

WanWizard commented 12 years ago

The update() method is a convenience method for $this->db->update(), which runs an update query over your entire table. It is difficult to run a validation rule on that, unless your rules are very generic. An option could be to implement force_validation() for that purpose, so you can enable validation at your own risk.

Queries in DataMapper are supposed to returns full or partial rows of a table, and the fields in the result must map back to the fields of the model. To run custom queries that meet that requirement, either code your own model methods, or use the query() method (or do both). It should be able to deal with stored procedures, as long as they return a single resultset. ExecuteScalar is an example of something that doesn't belong in DataMapper, it's result doesn't have any relation with the model.

WanWizard commented 12 years ago

Reply by @ingdiaz

Ok all the that you say is good, but the point of the update you see to much records, I see that if save one record enter to the validate is very good, but this validate is not very useful because if update the record that save, no enter to validate and jump the rules, the first rules of the save now are useless, are two view points different, you see the update to much records and I as only one record, that is most common.

Thankyou Great Work

WanWizard commented 12 years ago

Still unsure what you're trying to do. DataMapper allows you to do

$object->update('field', 'value');

which will generate

UPDATE table SET field = 'value';

you could run validation on the field passed, but only if you have very generic validation rules (for example ones that check the type, or do a trim on a string). If you have validation rules that deal with contents (for example, to check if the value is unique), you will introduce a corruption in your table since the validation will pass (when you check it, it is unique) but multiple records will end up with the same value.

That is why I propose not to do this automatically, but provide something like

$object->validate()->update('field', 'value');

so you can decide whether or not your validation rules are safe to use on this particular update statement.