Jump to content

database mapper - opinions needed on my approach


NetNerd85

Recommended Posts

Hey all,

 

I have developed a database mapper class and I am not sure if the design of it is very smart. The database mapper class covers the basic CRUD functions that most modules need. The thing about my database mapper class is that you pass the database table name of a "module" to the database mapper class. This will enable you to use the basic CRUD functions without having another class. Say you have a pages table, you instantiate the database mapper class like this:

 

$pages = new Data_Mapper('pages');
$pages->input_data($_POST);
$pages->create();
$all_pages = $pages->get_all(); 

 

As you can see there is no need for a pages module. The input_data function takes all information from $_POST and filters it, matches it against the database column information and builds the necessary queries. You can have custom queries, exceptions to ignore input etc.

 

It needs to have the specific column names in the $_POST array but this is not difficult to manage. You need to call them something in the HTML. You can always make a class to manage HTML generation too.

 

The base functions for CRUD are:

 

create() - insert record into the database
retrieve_record() - gets one record by id from db
retrieve_all() - gets all records from db
delete() - removes a record from the db

 

Other functions for setting up the data_mapper for usage are:

 

set_query() - use a custom query for CRUD functions
set_input_data() - set the data array to use with CRUD functions
set_input_exceptions() - set the exceptions for CRUD queries (such as when you insert a row in the database you will need to set an exception for the primary key)
set_primary_key() - this can be an array of keys, used with update and select statements

 

There are of course other functions but these are private for the data_mapper to build the queries only.

 

You will definitely need to extend the class at this point for advanced features. Say you have a categories module that you want to use for news, pages and files (aka downloads). You will need the category_id, the record_id and the module_id. You would extend the data_mapper and create the necessary function to insert a new category record for each module. This would be as simple as setting up a custom query.

 

class Pages extend Data_Mapper
{
  public function set_category_association($page_id, $category_id)
  {
    $this->set_query("INSERT INTO category_items (item_id, category_id, module_id) VALUES($page_id, $category_id, $this->module_id)");
    return $this->create();
  }
} 

 

You don't even need to extend the class you can do all that in the PHP file itself.

 

It has taken me a while to build this class, it does a lot but does it do too much? Should I stick to the Active Record pattern or something? My concerns are mainly about scalability and performance. I mean have I made this way too complex? Even if I have, is it "smart"? I just don't feel that it is a good design ???

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.