CptnChainsaw Posted November 16, 2012 Share Posted November 16, 2012 Hi, I'm wanting to design an interface to the database to make it easier to save/select records to the database etc. Using an off the shelf ORM or framework isn't an option at the moment. So I'm looking to build my own so that existing code can be ported over time. Currently I've got something like this: $data->setTables(array('pay_support_cases')); $data->setFields(array('firstname', 'lastname', 'email', 'question')); $data->setRequest($_POST); $data->save(); This simply saves the data in the $_POST array to the table and fields passed in to the set methods. Calling the save method simply generates a query and executes it. This is going to become problematic with lots of tables etc. So I would be looking for something like an object for each table so if I had a "user" table I could do: $user->save(); I'm not sure how best to go about this, can anyone give me any pointers? Cheers in advance Quote Link to comment https://forums.phpfreaks.com/topic/270785-database-interface-design/ Share on other sites More sharing options...
shlumph Posted November 16, 2012 Share Posted November 16, 2012 Keeping the active record approach it seems you're looking for, you could do something like this: <?php class User { private $_fields = array( 'id' => null, 'username' => null, 'email' => null //etc. ); public function setFields(array $data) { //Set data in $_fields array } public function save() { //save } //Other crud methods } Quote Link to comment https://forums.phpfreaks.com/topic/270785-database-interface-design/#findComment-1392963 Share on other sites More sharing options...
CptnChainsaw Posted November 16, 2012 Author Share Posted November 16, 2012 Cheers shlumph, I think that's what I'm looking for. I'll do some more thinking on this, got a feeling its enough to get me going though, thanks again Quote Link to comment https://forums.phpfreaks.com/topic/270785-database-interface-design/#findComment-1393045 Share on other sites More sharing options...
Christian F. Posted November 17, 2012 Share Posted November 17, 2012 The biggest issue, as far as I see, with the approach you've outlined above is going to be the validation of the input variables. Or rather, the lack of specific definition of where they are found and what they contain. Using the pattern as described by shlumph, to have the methods of the use-specific (controller) class to the retrieval and validation, is probably going to be the least complex solution. That way each class knows everything it needs to know about each member, where to get it, and how it's validated. Then the controller class itself can set the proper parameters to the model (database interface) in pretty much the same way your described above. Just without the $_POST bit. Quote Link to comment https://forums.phpfreaks.com/topic/270785-database-interface-design/#findComment-1393150 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.