Jump to content

Database Interface Design


CptnChainsaw

Recommended Posts

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 :)

Link to comment
Share on other sites

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
}

Link to comment
Share on other sites

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. ;)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.