Welcome Guest, Not a member yet? Register   Sign In
AutoModel 0.1
#1

[eluser]Developer13[/eluser]
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:

Code:
<?

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:

Code:
// 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.
#2

[eluser]bijon[/eluser]
Very cool and nice .
#3

[eluser]m4rw3r[/eluser]
This seems like a nice little addition to make it easier to get models up and running faster.

But why does your code always pull it's data from post?

EDIT: removed unnecessary question
#4

[eluser]wiredesignz[/eluser]
I really like this Jesse, Good work man. Wink
#5

[eluser]Jamie Rumbelow[/eluser]
Yeah, nice idea, and excecuted well.
#6

[eluser]Colin Williams[/eluser]
I too have been contemplating this idea, but I think we all have! Diggin this a lot. Nice one.




Theme © iAndrew 2016 - Forum software by © MyBB