Jump to content


Database Interface Design

database object oriented design

  • Please log in to reply
3 replies to this topic

#1 CptnChainsaw

  • Members
  • PipPip
  • Member
  • 12 posts

Posted 16 November 2012 - 12:16 PM


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->setFields(array('firstname', 'lastname', 'email', 'question'));

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:


I'm not sure how best to go about this, can anyone give me any pointers?

Cheers in advance :)

#2 shlumph

  • Members
  • PipPipPip
  • Advanced Member
  • 576 posts
  • LocationNew Hampshire

Posted 16 November 2012 - 02:34 PM

Keeping the active record approach it seems you're looking for, you could do something like this:

class User
    private $_fields = array(
        'id' => null,
        'username' => null,
        'email' => null

    public function setFields(array $data)
        //Set data in $_fields array

    public function save()

    //Other crud methods

whether you think you can, or can't, you're right -HF
Visit my website or github

#3 CptnChainsaw

  • Members
  • PipPip
  • Member
  • 12 posts

Posted 16 November 2012 - 07:36 PM

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

#4 Christian F.

Christian F.
  • Staff Alumni
  • Advanced Member
  • 3,106 posts
  • LocationNorway

Posted 17 November 2012 - 05:32 AM

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. ;)
Keeping it simple.

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users