Designing a general database interface in PHP


Question

I'm creating a small framework for my web projects in PHP so I don't have to do the basic work over and over again for every new website. It is not my goal to create a second CakePHP or Codeigniter and I'm also not planning to build my websites with any of the available frameworks as I prefer to use things I've created myself in general.

I have had no problems in designing and coding the framework when it comes to parts like the core structure, request handling, and so on, but I'm getting stuck with designing the database interface for my modules.

I've already thought about using the MVC pattern but found out that it would be a bit of an overkill for my rather small project(s).

So the exact problem I'm facing is how my frameworks modules (viewCustomers could be a module, for example) should interact with the database.

  • Is it (still) a good idea to mix in SQL directly into PHP code? (Would be "old way": mysql_query( 'SELECT firstname, lastname(.....))?

  • How could I abstract a query like the following?

    SELECT firstname, lastname FROM customers WHERE id=X
    

Would MySQL "helper" functions like

$this->db->customers->getBy( 'id', $x );

be a good idea?

I'm not really sure because they tend to become useless when dealing with more complicated queries like the pretty much trivial one above.

  • Is the "Model" pattern from MVC my only real option to solve this?

  • What do you currently use to solve the problems shown above?

1
11
3/21/2010 9:05:53 PM

Accepted Answer

If you need speed, then use raw queries (but you should really use PDO with prepared queries).

If you want something more OOP, you can —as you suggest it— design this with helpers.

Once, I've designed something similar which had the following concept:

  1. DB connection/handler classes (handling multi-connections to different databases and different servers such as MySQL, Oracle, etc.);
  2. A class per action (ie. SELECT, DELETE, etc.);
  3. Filter classes (eg. RangeFilter);

The code looked something like this:

$select = new Select('field1', 'field2', );
$result = $select->from('myTable')
                 ->addFilter(SQLFilter::RangeFilter, 'field2')
                 ->match(array(1, 3, 5))
                 ->unmatch(array(15, 34))
                 ->fetchAll();

It's a simple example of how you can build it.

You can go further and implements automated handling of table relations, field type check (using introspection on your tables), table and field alias support, etc.

It might seem to be a long and hard work, but actually, it won't take you that much time to make all these features (≈1 month).

3
3/28/2010 9:50:46 AM

I believe you just want to get access to your DB from your module. I'd avoid using mysql_query directly from the code. Rather, going for simple model with abstracted DB access would be easy and straight-forward.

For example, you can have a file like models/Customers.php with this code:

<?php

class Customers {

    public function getById($id) {
        $sql = "SELECT first_name, last_name FROM customers WHERE id='$id'";
        $res = $DB::getRow($sql);
        return ($res);
    }
}

I am assuming some kind of DB helper is already instantiated and available as $DB. Here is a simple one which uses PDO.

Now, you should include this in your module and use the following way:

<?php

include_once "models/Customers.php";

$customers = new Customers();
$theCustomer = $customers->getById(intval($_REQUEST['cust_id']));


echo "Hello " . $theCustomer['first_name']

Cheers.


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon