Jump to content


Photo

Database Interface Design

database object oriented design

  • Please log in to reply
3 replies to this topic

#1 CptnChainsaw

CptnChainsaw

    Member

  • Members
  • PipPip
  • 12 posts

Posted 16 November 2012 - 07:16 AM

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

#2 shlumph

shlumph

    Advanced Member

  • Members
  • PipPipPip
  • 576 posts
  • LocationNew Hampshire

Posted 16 November 2012 - 09:34 AM

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
}

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

#3 CptnChainsaw

CptnChainsaw

    Member

  • Members
  • PipPip
  • 12 posts

Posted 16 November 2012 - 02: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.

    Advanced Member

  • Staff Alumni
  • 3,106 posts
  • LocationNorway

Posted 17 November 2012 - 12: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

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com