MY_Model 2.0.0 At A Glance
Tuesday, September 11, 2012 at 4:49PM Hot off the presses is the news that version 2.0.0 of my MY_Model base model for CodeIgniter has been released. 2.0.0 brings a huge range of new features, as well as a bunch of internal code changes, that I'll go through here.
Relationships
By far the biggest addition to MY_Model is support for basic relationships. Better relationship / JOIN support has been a widly requested feature pretty much from day one; I feel like I've come up with a simple and elegant solution to the problem.
It's not particularly performant and only supports one-to-many relationships, but for the occasions when you need a really simple and speedy way of associating rows, it's perfect.
Relationships are defined in the $belongs_to and $has_many instance variables:
class Post_model extends MY_Model
{
public $belongs_to = array( 'author' );
public $has_many = array( 'comments' );
}
You can then use the with() scope method to add any of the defined associations to the result:
$post = $this->post_model->with('author')
->with('comments')
->get(1);
echo $post->author->title;
echo count($post->comments);
Under the hood, this adds a new $after_get observer that calls the related model. This means that the result is embedded in the object, but that it suffers from the n+1 problem. If performance is important, or n+1 is biting you, it's still recommended to manually write a join in a custom method.
You can also customise the name of the loaded model and used primary key. These are sensible defaults, but can be overridden, so check out the relationships section of the documentation for more on this.
Serialisation / Serialization
If you've got some arbitrary data to insert--a data matrix, for instance--that you don't wish to set up in a traditional relational format, but doesn't require a non-relational database, you can use serialisation.
MY_Model now contains two new built-in observers, serialize() and unserialize(). Simply add them in $before_create and $after_get and watch as you seamlessly pass in complex data structures with ease:
class Event_model extends MY_Model
{
public $before_create = array( 'serialize(matrix)' );
public $after_get = array( 'unserialize(matrix)' );
}
$this->event_model->insert(array(
'matrix' => new RandomMatrixObject
));
get_class($this->event_model->get(1)->matrix) == 'RandomMatrixObject' // TRUE
You can pass in absolutely anything you like and it'll be serialised appropriately. Customise the serialisation behaviour of classes by using __sleep and __wakeUp.
Soft Delete
Sometimes you don't want to destroy the row in the table, just remove it from the context of the application. Soft delete comes to the rescue. Simply set $soft_delete to be TRUE and rows will magically be marked as deleted:
class Account_model extends MY_Model
{
public $soft_delete = TRUE;
}
Protected Attributes
I've been very lazy and have been throwing data straight into most of my models from the HTML form. This is, as I'm sure you can imagine, unimaginably dangerous and rather stupid.
Not to worry! Protect those attributes you care most about and then be reckless worry-free!
class Post_model extends MY_Model
{
public $protected_attributes = array( 'id', 'hash' );
}
$this->post_model->insert(array(
'id' => 2,
'hash' => 'aqe3fwrga23fw243fWE',
'title' => 'A new post'
));
Much More
There are tonnes of other changes under the hood and new features that I've not talked about here. A few more bits I've not explained but are worth a quick mention nonetheless:
- The removal of Composer support. I love Composer but it was causing problems inside CI and until CI starts treating Composer properly I'll leave it out.
- Massive internal change to the way callbacks work. Might break existing code.
- Built-in timestamps (created_at and updated_at)
- Manual DB override
- A bunch of bugfixes
Contributors
Getting to a powerful, stable 2.0 has been an almighty challenge, and is the summation of a lot of people's contributions and time. A massive, Eric Pickles-sized thank you to the following people (in no particular order):
Want To Contribute?
Get onto GitHub, fork away, branch, write the code and unit tests(!) and submit a pull request. It'd be awesome to get even more contributions. Together we can build a really great base model for CodeIgniter.
Thanks again to everyone who's helped out with their feedback, support and coding skillz, and get over to GitHub now and grab a copy of MY_Model 2.0.0!
Love you all. Peace out.
codeigniter,
php,
programming 


Reader Comments (2)
Can you please help with http://stackoverflow.com/questions/12839037/using-relationship-in-jamie-rumbelow-my-model/12847020#12847020
How do i insert this using MY_Model, where the array keys (persons, students ) are models
$data = array (
'persons' => array(
'firstname' => 'john',
'lastname' => 'doe',
'othernames' => 'Kabiri',
'email' => 'jondoe@yahoo.com',
'students' => array(
'intake' => 'BCA-2009',
'admission_number' => 'BCA-001-029',
'date_of_admission' => strtotime('now'),
'former_school' => 'Former School',
)
)
);