AutoModel 0.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: <? 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: 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.
[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
[eluser]Colin Williams[/eluser]
I too have been contemplating this idea, but I think we all have! Diggin this a lot. Nice one. |
Welcome Guest, Not a member yet? Register Sign In |