CodeIgniter Forums
MY_Model Base CRUD Model - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Libraries & Helpers (https://forum.codeigniter.com/forum-22.html)
+--- Thread: MY_Model Base CRUD Model (/thread-24361.html)

Pages: 1 2 3 4 5 6


MY_Model Base CRUD Model - El Forum - 11-08-2009

[eluser]Jamie Rumbelow[/eluser]
I've recently released an extended Model class to use in CodeIgniter applications that provides a base CRUD pattern for your models. It gives you the core CRUD functionality, as well as support for before_create and after_create callbacks and some clever table name guessing.

Usage
Drag the MY_Model.php file into your application/libraries folder. CodeIgniter will load and initialise this class automatically for you. Then all you have to do is make sure all your models extend from MY_Model and all the functionality will be baked into your models automatically!

Tutorial
I've written a tutorial that takes a reasonable look at the functionality provided by the entire class, as well as my MY_Controller Controller System on my delightful blog. Check it out and feel free to leave a comment.

Download
You can get the source directly from GitHub, where I'll be updating the library. You can also fork it and customise it yourself! Alternatively, you can download it as a .zip or a .tar.

Naming Conventions
This class will try to guess the name of the table to use, by guessing the plural of the class name. If the table name isn't the plural and you need to set it to something else, just declare the $table instance variable and set it to the table name. Some of the CRUD functions also assume that your primary key ID column is called _'id'_. You can overwrite this functionality by setting the _$primary\_key_ instance variable.

Upcoming Features
* Support for validations
* Function reference
* Before and after update callbacks
* Better table name guessing
* Better support for associations and JOINs

Version History
* 1.1.0 Two new methods, count_by() and count_all(), made use of some ActiveRecord methods and optimisations
* 1.0.5 Added support for custom primary keys
* 1.0.0 First release


MY_Model Base CRUD Model - El Forum - 11-08-2009

[eluser]hugle[/eluser]
Thanks,

nice pack of functions!


MY_Model Base CRUD Model - El Forum - 11-08-2009

[eluser]imn.codeartist[/eluser]
Quote:Naming Conventions
This class will try to guess the name of the table to use, by guessing the plural of the class name. If the table name isn’t the plural and you need to set it to something else, just declare the $table instance variable and set it to the table name. Some of the CRUD functions also assume that your primary key ID column is called ‘id’.

Well there are also lot more people who don't prefer calling primary key id as "Id".. so in that case your library is not useful right ??


MY_Model Base CRUD Model - El Forum - 11-08-2009

[eluser]Jamie Rumbelow[/eluser]
[quote author="dixcoder" date="1257721137"]
Quote:Naming Conventions
This class will try to guess the name of the table to use, by guessing the plural of the class name. If the table name isn’t the plural and you need to set it to something else, just declare the $table instance variable and set it to the table name. Some of the CRUD functions also assume that your primary key ID column is called ‘id’.

Well there are also lot more people who don't prefer calling primary key id as "Id".. so in that case your library is not useful right ??[/quote]

Not at all. That's just the way I do things. If you've got an issue with that just change it.


MY_Model Base CRUD Model - El Forum - 11-08-2009

[eluser]Jamie Rumbelow[/eluser]
I just updated the class to allow for custom primary key names - by default it will assume your primary key is "id" but you can now change that by setting the $primary_key instance variable to the primary key of your table.

Jamie


MY_Model Base CRUD Model - El Forum - 11-09-2009

[eluser]Boris Strahija[/eluser]
I think it would be really great if there would be a save() method, so something like this would be possible:
Code:
$post = new Post();
$post->title = 'Lorem ipsum';
$post->body = 'Test...';
$post->save();

I was working on somethig very similar but lately I don't have so much time Wink

Oh, and please use tabs instead of spaces for code indenting Smile


MY_Model Base CRUD Model - El Forum - 11-09-2009

[eluser]Jamie Rumbelow[/eluser]
Boris,

You're looking for an ORM! My MY_Model is merely a CRUD base and won't ever be that advanced to support ORM and relationships etc. It's just a quick and easy way of reusing replicated functionality across your models.

Jamie


MY_Model Base CRUD Model - El Forum - 11-09-2009

[eluser]Boris Strahija[/eluser]
I wasn't talking about an ORM, just the part for saving objects seems to be a nice addon.
Relationships are of course an overkill for a simple class like that.
My extended Model class has something like this:
Code:
function MY_Model()
{
    ...
    
    parent::Model();
            
    // Get the DB table name
    $this->_get_table_name();
            
    // Load all fields if table exists
    $this->_get_table_fields();
    
    ...
} //end __contruct()

Getting the fields:
Code:
private function _get_table_fields()
{
    if ($this->db->table_exists($this->table)) :
        $this->fields = $this->db->list_fields($this->table);
    else :
        echo "The table '", $this->table, "' does not exist.";
        exit();
    endif;
    
} //end _get_table_fields()

And the save method:
Code:
function save($id = NULL)
{
    foreach ($this->fields as $key=>$field) :
        if (isset($this->$field)) $data[$field] = $this->$field;
    endforeach;
            
    // if ID is set update a record
    if ($id || isset($this->id)) :
        if ($id) $this->update($id, $data);
        else $this->update($this->id, $data);
    else :
        return $this->insert($data);
    endif;
    
} //end save()

That's it, nothing fancy Wink


MY_Model Base CRUD Model - El Forum - 12-02-2009

[eluser]Jamie Rumbelow[/eluser]
[quote author="Boris Strahija" date="1257812890"]I wasn't talking about an ORM, just the part for saving objects seems to be a nice addon. Relationships are of course an overkill for a simple class like that.[/quote]

That sounds lovely, but sorry, it's a bit too much of an ORM for my liking Wink The idea behind this is to provide a simple CRUD interface with a few niceties, nothing more. Otherwise you get into the realms of developing an ORM and that's a totally different project.

Jamie


MY_Model Base CRUD Model - El Forum - 12-02-2009

[eluser]Jamie Rumbelow[/eluser]
Update 1.1.0
Tidied up a bit, added two new methods count_by() and count_all(), moved some stuff over to some ActiveRecord methods I'd overlooked changed the syntax a bit etc. Had a lot of help with Phil Sturgeon on this one, so many thanks to him. I've also updated the first post with a link to a tutorial found on my blog.

Cheers!