Reputation: 14314
When developing bigger systems in team, often, there are separate guys for implementing application and database stuff. In such cases, database design and implementation appears first: database developer designs and creates tables with some modeling tool. As an application developer, you are tempted to create and add the rules from scratch in your app code. However, then you are duplicating the work, because actual column type and length restrictions are already set in every DB table. Also, if database developer is responsible and is updating the DB then there might happen bugs when you forget to update the rules in app side. So, you just need to grab those rules from existing database schema tables and "mount" to your input validation (or model) classes.
So, the question is are there any general approaches (and examples), libraries or frameworks that show how this should be done. I'v looked at Codeigniter framework that has rules, but those rules are not captured from existing db, you need to define them explicitly.
PS. Most preferable would be simple (but sufficient) solutions because I already use a lightweight Codeigniter-like framework, and I don't want to rely on heavyweight approaches.
Upvotes: 0
Views: 383
Reputation: 707
You could try Zend_Db_Table_Abstract. While the Zend framework requires some initial setup, you can use only the pieces you require for your project (as opposed to some other PHP frameworks). You can use the describeTable() method to discover something about the table structure, and you can overload the insert() function to explicitly check types, or do whatever other validation you require.
Another potential option is fSchema, part of the flourish library. I have had good experiences with flourish in the past.
Both are modular, well documented, and easy to use.
Upvotes: 1