Inside a CakePHP application, for unique constraints which are paid for for within the database, what's the advantage of getting exactly the same validation inspections within the model?

I realize the advantage of getting JS validation, however i believe this model validation bakes an extra visit to the DB. I'm 100% certain certain validations come in the DB therefore the model validation would just be redundant.

The only real benefit I see may be the application realizing the error and modifying the vista for that user accordingly (repopulating the fields and showing error message around the appropriate area improving the ux) but this may be accomplished if there is a constraint naming convention so the application could know very well what the issue was using the save (existing method to get this done now?)

Faster response occasions, less database load. The further to the customer that you can do validation, i.e. JavaScript, the faster it's. The main disadvantage is needing to implement exactly the same rules in multiple layers.

Nearly any benefit which you may gain would most likely be canceled by the need for maintaining the contraints in duplicate. Unless of course you have a simple mechanism for indicating these questions single location and keeping them synchronized, I would suggest adhering with one location from the other.

If database constraints are coded by one individual and also the relaxation from the code is code by another, they should not trust one another completely. Check things at limitations, particularly if they represent business-people limitations. e.g. user to application a treadmill designers module to a different, a treadmill corporate department to a different.

Validation in CakePHP happens before save or update totally delivered to the database. So that it cuts down on the database load. You're wrong inside your belief the model validation bakes an extra visit to the database. By default, validation occurs before save.

Remember the problem of portability. Enforcing validation within the model keeps the application database-agnostic. You are able to program a credit card applicatoin against a SQLite database, after which deploy to MySQL.. oh wait, you do not have that.. PostgreSQL? No? Oh, Oracle, fine.

Also, being produced, if your database error happens on the typical controller action that saves after which redirects, your user is going to be stuck looking in a blank whitened page (since errors are off, there is no view to output, and also the redirect never happened). Essentially, database errors are switched off being produced mode as they possibly can give understanding of the DB schema, whereas model validation errors remain enabled because they are user-friendly.

You've got a point though, can you really capture these database errors and make a move helpful together? Presently no, but it might be nice if CakePHP could dynamically translate them into unsuccessful model validation rules, stopping us from repeating ourselves. Different databases throw different searching errors, so each DBO datasource would want up-to-date to aid this before it might happen.

Ideally the style of the model should come first (according to user tales, use cases, etc.) using the database schema drawing in the model. Then your database implementation may either be produced in the model (clearly tying both to some single source), or even the database constraints could be designed according to relational integrity needs (that are conceptually not the same as, and usually possess a different granularity and vocabulary than, the model, although oftentimes there's a mapping of some type.

I generally are thinking about only relational integrity needs for database constraints. You will find a lot of cases when the granularity and universal usefulness of economic constraints are extremely incongruent, transitory, and finer-grained compared to database designer is aware of plus they change more often with time and across application modules.