Part of the EllisLab Network
   
1 of 4
1
ORM - Object Relational Mapper library | pending major updates
Posted: 01 May 2007 10:38 AM   [ Ignore ]  
Lab Assistant
Avatar
RankRank
Total Posts:  110
Joined  09-17-2004

Last updated: 3rd June, 2007
Some major updates are coming soon. smile


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. smile

======================================================================================

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. wink

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. smile

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. smile

Thank you all and hopefully this library will become useful to some people in the near future!

Cheers,
Fred

 Signature 

Fred Wu
Founder of Wuit.com | Web Developer at Envato
My blog: fredwu.me | Follow me @fredwu

Profile
 
 
Posted: 01 May 2007 10:40 AM   [ Ignore ]   [ # 1 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  110
Joined  09-17-2004

Source code

http://thislab.com/temp/ORM.phps

========================================================================================

Changelog (after being published here):

[14/05/2007] Rev: 19

* updated the comments to reflect the change in recordsCount()

[14/05/2007] Rev: 18

* fixed orm_schemas table structure in comments
- removed the conflicting method tableCount()
* enhanced recordsCount()

[09/05/2007] Rev: 17

* License changed to LGPL!

[05/05/2007] Rev: 16

* corrected the value of findCount
+ added recordsCount()

[03/05/2007] Rev: 15

* fixed some typo in comments
* fixed a bug with $this->__conditions
+ added subversion meta data

[02/05/2007] Rev: 14

* fixed a bug for conditional queries

 Signature 

Fred Wu
Founder of Wuit.com | Web Developer at Envato
My blog: fredwu.me | Follow me @fredwu

Profile
 
 
Posted: 06 May 2007 04:47 AM   [ Ignore ]   [ # 2 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  101
Joined  10-01-2006

this is really neat, great job!

 Signature 

Check out the BlueFlame Project!

Profile
 
 
Posted: 07 May 2007 02:46 AM   [ Ignore ]   [ # 3 ]  
Summer Student
Total Posts:  20
Joined  09-21-2006

Hi, Canglan,

Not sure where is the problem, but here’s what is happening:
On my laptop, when I load the orm library, some weird characters appear before the layout gets loaded, characters that brake my layout.

On the same config, but on other PC, everything is OK.

Any ideea about this?

Thanks.

Profile
 
 
Posted: 07 May 2007 02:49 AM   [ Ignore ]   [ # 4 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  110
Joined  09-17-2004

Hi stefanm, are you able to take a screenshot?

On the same config, but on other PC, everything is OK.

Everything is the same including operating system, web server, php and mysql version?

 Signature 

Fred Wu
Founder of Wuit.com | Web Developer at Envato
My blog: fredwu.me | Follow me @fredwu

Profile
 
 
Posted: 07 May 2007 03:20 AM   [ Ignore ]   [ # 5 ]  
Summer Student
Total Posts:  20
Joined  09-21-2006

Yes, same config on both machines.

Windows 2003
Apache2Triad version 1.5.4
Apache version 2.2.0
MySQL version 5.0.18
PostgreSQL version 8.1.2
Openssl version   0.9.8a
Slimftpd version 3.18

Profile
 
 
Posted: 07 May 2007 03:24 AM   [ Ignore ]   [ # 6 ]  
Summer Student
Total Posts:  20
Joined  09-21-2006

Weird: the garbage is only shown in Opera 9 and Firefox 2.
IE 6 is OK.

Geez…

Profile
 
 
Posted: 07 May 2007 04:37 AM   [ Ignore ]   [ # 7 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  110
Joined  09-17-2004

Unfortunately it’ll be very difficult if possible at all to trace the cause of this without any further information such as a screenshot. :(

You might want to try echo some stuff throughout your PHP file (just like how we debug our javascript using alert()) to see *where* do the garbage come from.

 Signature 

Fred Wu
Founder of Wuit.com | Web Developer at Envato
My blog: fredwu.me | Follow me @fredwu

Profile
 
 
Posted: 07 May 2007 04:38 AM   [ Ignore ]   [ # 8 ]  
Summer Student
Total Posts:  20
Joined  09-21-2006

I’ve sent you some screenshots. Didn’t receive them?

Profile
 
 
Posted: 07 May 2007 06:16 AM   [ Ignore ]   [ # 9 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  110
Joined  09-17-2004

Whoops sorry I didn’t notice there was a private message. smile

Okay, I’ve tried to install Apache2Triad, unfortunately it doesn’t work on Windows Vista. Instead I’ve installed Xampp, and everything is fine.

If you are absolutely certain that you didn’t miss type anything in your code, then I’d suggest you maybe try out Xampp (Xampplite is only 18MB) and see if it still has this issue? It seems to be possible that Apache2Triad is the issue.

If you decided to give Xampp a go, make sure after you’ve extracted the file, run ‘setup_xampp.bat’, then make two small changes to the httpd.conf (located at apache/conf): search for ‘mod_rewrite’ and uncomment the line, search for ‘AllowOveride’ and change ‘AllowOveride None’ to ‘AllowOveride All’.

 Signature 

Fred Wu
Founder of Wuit.com | Web Developer at Envato
My blog: fredwu.me | Follow me @fredwu

Profile
 
 
Posted: 08 May 2007 12:05 PM   [ Ignore ]   [ # 10 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  351
Joined  07-25-2006

Ah, I am about to test this out, however, I have one problem: it’s GPL licensed. This makes it impossible for me to make fixes to this in a commercial environment. Please release under an license that allows commercial use: BSD, MIT, or LGPL. It’s completely unfair to people that build commercial web applications to license under GPL, because we can never edit your code or write commercial applications based on it.

 Signature 

me and some random code, hosted by dh. and a blog too! ++ dead bugs

Profile
 
 
   
1 of 4
1
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 819, on March 11, 2010 11:15 AM
Total Registered Members: 120018 Total Logged-in Users: 38
Total Topics: 126155 Total Anonymous Users: 3
Total Replies: 663598 Total Guests: 360
Total Posts: 789753    
Members ( View Memberlist )