AutoModel 0.1

Hey all,

I’ve been meaning to do this for some time now and I finally took a few minutes tonight to put this together.  AutoModel allows you to define a model with only one function (the constructor).

Let’s take a look at a model that uses AutoModel:


class mdl_entries extends My_Model {

    
// make sure to extend MY_Model

    
function mdl_entries() {

        
// call the parent constructor
        
parent::__construct();

        
// define the table name (required)
        
$this->datadef['table'] = 'entries';
        
        
// what is the primary key?  (required)
        
$this->datadef['primary_key'] = 'entries.id';

        
// datadef['joins']['table_name_to_join'] = 'join_syntax' (NOT required)
        
$this->datadef['joins']['sites'] = 'sites.id = entries.site_id';

        
// and here are the fields for SELECT queries (required)
        
$this->datadef['fields'] = array(
        
'entries.id',
        
'entries.site_id',
        
'entries.author_id',
        
'entries.date',
        
'entries.published',
        
'entries.title',
        
'entries.content',
        
'sites.name');

    
}

}

?>

That’s it for the model!  This child model is now a simple definition of our table structure and any joins needed for retrieving data.  We define the name of the primary key for delete() and save() purposes.

Notice I use tablename.fieldname.  I do this to avoid ambiguous column name problems (such as joining a table and using ‘WHERE id = 5’ when two tables exist in the join with a column named ‘id’).  Plus it just makes it more readable.

Now let’s take a look at how to use AutoModel from a controller:

// to pull all entries:
$all_entries = $this->mdl_entries->get();

// to pull only one entry by a primary key value:
$entry = $this->mdl_entries->get($key_value);

// to save a new entry:
$this->mdl_entries->save();

// to save an existing entry based on the primary key value:
$this->mdl_entries->save($key_value);

// to delete an entry based on the primary key value:
$this->mdl_entries->delete($key_value);

For save() to work, you’ll need to make sure your form field names are the same as your database field names.  You don’t need to pass anything to it for it to function—it looks through $this->datadef[’fields’] and matches any existing $this->input->post() values.  If a value exists, it will add it to the list of fields to be saved; if not, it will ignore it.

Currently the only way to delete or select a particular item is by the table’s primary key.  I’ve only worked on this for just a little bit tonight—but I do plan to make it capable of selecting / deleting items based on a different key when needed.

To use AutoModel:

1 - Download the file
2 - Unzip and drop in to your system/application/libraries folder
3 - Make sure your new models extend MY_Model

That’s it!  Models created using AutoModel only need the constructor function with the required data definition.  Have fun and please provide input!  I don’t like having to rewrite all of the same functions into all of my models—but I also don’t want a rails approach either where it does everything for me… so this is a solution for making it easier without compromising flexibility.

Posted in CodeIgniter by Jesse Terry on 08/21/2008 | 4 Comments

Comments:

Md Emran Hasan on 10/04/2008 `at` 08:13 AM:

Looks good. I have a similar implementation, where you need to pass two lines in the model constructor: // Call the Model constructor parent::Model(); // Load the associated table $this->loadTable('products'); Check it out here: http://www.phpfour.com/blog/2008/07/12/extended-model-for-codeigniter/ Thanks!

Ray on 10/01/2008 `at` 02:44 AM:

Hi Jesse, I like the concept you have chosen, especially how the only real query type you have to specify is the fields for the select query. It sort of brings it into an ORM type situation. What other features will you eventually be planning on?

Jesse Terry on 09/04/2008 `at` 02:27 AM:

Hey Jonathon - Thanks for the note and for the bug report! Automodel is a very young conception that took me about an hour to write. I certainly appreciate the suggestion and I think it's a great idea! Thanks!

Jonathon Hill on 09/03/2008 `at` 12:28 PM:

Hey Jesse, cool idea! I would like to optionally pass save() an array of data to insert (what if I have to pre-process the POST data before inserting it)? You could make it an optional arg and use $this->input->post if it isn't present. Also, your download link results in a DB error (you might also want to turn off debug mode in your database.php config file so you don't expose db info in such a situation).


Search This Site


Categories

Blogroll

Users