Last updated: 3rd June, 2007
Some major updates are coming soon. ![]()
Source code can be found in the second post.
Hi folks,
Having been working on this for two weeks (including one week’s work being lost), finally I have something to show you as an early alpha.
Disclaimer: This is a very early alpha version which has NOT been tested thoroughly, so it will have bugs.
Notice: This post is kinda brief, I’ll progressively improve it so don’t panic just yet. ![]()
======================================================================================
What is ORM?
For a lengthy explanation, please visit this wikipedia article. In short, it maps your database tables and fields with their relationships to programming objects.
Why ORM?
CodeIgniter is very simple to use, however one thing it lacks is an ORM layer. If you’re familiar with Ruby on Rails or CakePHP then you will know the power of ORM. It handles model data with its related data, so you don’t have to worry about associations as long as you follow the convention.
Disadvantages
I figure I should state the disadvantages. ![]()
You will lose a certain degree of freedom because your database HAS to be constructed in a way which the ORM library will understand.
If you have already established your application, then it will be impossible to use this library without a major refactoring of your existing code and database.
And a disadvantage for presence: don’t use it just yet, because it’s only in alpha stage!!
Overview
Basically, this ORM library is inspired by Ruby on Rails’s Active Record class and CakePHP’s model class.
It offers (most) features offered in RoR and CakePHP with convenient enhancements!
Highlights
- Convention over configuration
- No need to declare your model associations!
- Automatically handles various relationships (belongs to, belongs to itself, has one, has many, has and belongs to many)
- simplicity (put the library in your app’s library folder, autoload it, off you go!)
- cached schema, only builds the schema once then stores them in the database
How to use it
- check the instructions provided in the source, follow the requirements (you need to follow the database convention or it will not work at all)
- put the file (ORM.php) in your app’s library folder (system/application/libraries)
- autoload it in system/application/config/autoload.php , database is required!
$autoload['libraries'] = array('database', 'orm')
======================================================================================
Usage examples
Imagine we have a discussion board system.
I want all of the posts with their associated data (users, categories and tags, etc):
$this->orm->find('posts');
And if I want the associated data of the users, categories and tags associated with posts. (two level of associations)
$this->orm->find('posts', null, 1, 2);
Okay I only want 30 posts:
$this->orm->find('posts', 30);
In a discussion board we’ll have pages, I want 30 posts for the second page:
$this->orm->find('posts', 30, 2);
What about finding a certain post? Say, I want the post with a post title of ‘my post’:
$this->orm->where('post_title', 'my post');
$this->orm->find('posts');
The previous code looks too long, can I shorten it? Yes you can!
$this->orm->findPost_title('my post', 'posts');
Well, it is shorter, but isn’t it redundant to have ‘post_title’ instead of ‘title’ as the field name? Yes, you can shorten the field name too!
$this->orm->findTitle('my post', 'posts');
Right, so I can find the exact post, but what if I want to perform a partial match? Yup, of course you can. The code below will find every post which contains ‘my post’ in the post title.
$this->orm->searchTitle('my post', 'posts');
Cool, how do I edit a post?
$this->orm->editPost(2);
$this->orm->post->title = 'edited title';
$this->orm->post->body = 'this post has been edited';
$this->orm->save();
What about delete?
$this->orm->deletePost(2);
Alias:
$this->orm->delPost(2);
What about create/add a record?
$this->orm->post->title = 'new post';
$this->orm->post->body = 'just inserted a new post yah!';
$this->orm->createPost();
‘createPost()’ has an alias too:
$this->orm->addPost()
======================================================================================
Thoughts
Well, I am pretty sure there are bugs and missing features, so please feel free to give any feedback or critics. ![]()
Not sure about performance, but the schemas (used for associating data) are cached in the database.
Note: The ORM schema is stored in the database table ‘orm_schemas’, if you’ve modified your database structure you’ll have to delete the corresponding records.
This post will be updated according later when there’re changes to the library. ![]()
Thank you all and hopefully this library will become useful to some people in the near future!
Cheers,
Fred
