New ORM, overview of changes
- Posted by dlib on July 16th, 2008 filed in Kohana, ORM, Subversion
If you look at Kohana trac you’ll see a lot of changes to the ORM library. As of, r3054 a new version of the ORM library has replaced the older version. In the next release of Kohana this new ORM will be standard. The big list of changes can be seen here.
The goal of Kohana ORM is to provide the typical ORM functionality but in a way that’s lean. If you need a full-fledged solution I suggest you use Doctrine which is extremely powerful. However, Doctrine consists of dozens of files, Kohana only needs a couple.
So, what are the important changes?
First of all the find_(all)_by_? syntax is gone. It uses __call() which is three times slower than ordinary method call. In your code change ‘find_all_by_email(’info@example.com’) by where(’email’=>’info@example.com’)->find_all(). The latter syntax was already available but now is standard. Also, find_related_?() is no longer there.
Secondly, ‘belongs_to_many’ is no longer available. $has_and_belongs_to_many now works both ways. The pivot table is now alphabetical: ‘article_user’ and not ‘user_article’. Has many through is now also supported. I’ll write a post on these two shortly I hope.
- where_key() is now unique_key()
- These read-only properties are available:
- ‘primary_key’, ‘primary_val’, ‘table_name’, ‘table_columns’, // Table
- ‘loaded’, ’saved’, // Status
- ‘has_one’, ‘belongs_to’, ‘has_many’, ‘has_many_and_belongs_to’, // Relationships
These are the changes in short. I’ll cover some of them in the future. Especially concerning relationships. This ORM is quite a bit more powerful than the last one and allows for greater extensibility. There is a bit of documentation available. A lot of stuff is the same or works in a similar way so now worries. I probably left some stuff out but that’s because I haven’t had the time to get to the knitty-gritty of the library and there’s quite a demand for more information on the subject so I was in a hurry ;). Please leave comments on stuff that’s not clear, or do some clarification yourself.
I will try to update some older posts to support new syntax but if you come across some old stuff please leave a comment so I can address it. Makes my job easier
Update: seems many people have problems with finding related records. It’s simple
$articles=$old_orm->find_related_articles();
$articles=$new_orm->articles;
Mind the pluralization though: has many plural, has one singular
July 16th, 2008 at 5:25 pm
How to initialise related? Does this depend on has_many_and_belongs_to??
July 16th, 2008 at 5:42 pm
OK - I figured it out ($user->roles), but how to add new related?
July 16th, 2008 at 9:30 pm
three times slower ? do you mean faster?
July 16th, 2008 at 10:57 pm
No, slower. __call() is 3 times slower than some_method()
July 17th, 2008 at 12:13 am
Thanks for the update dlib and I have to say, top marks to you and the team for a worth update to ORM. I’ve been using it for a couple of days and I can say it certainly feels more logical at this end.
Samsoir
July 21st, 2008 at 4:24 pm
Hello!
I’m pretty new to Kohana and ORM. In the ORM.php file I see in one place the relationship is has_and_belongs_to_many and in other place is has_many_and_belongs_too . Is that correct? Or they should be named the same?
July 21st, 2008 at 5:10 pm
“First of all the find_(all)_by_? syntax is gone”
This is kind of a dumb move, to be honest, but okay
July 21st, 2008 at 5:23 pm
codebirth: well spotted! I’ll brief Shadowhand on this.
Jon: I intend to show how you regain this behaviour with __call() in a short tutorial
July 24th, 2008 at 4:54 pm
dlib - great! Thank you!
July 31st, 2008 at 10:47 pm
does the new ORM no longer handle has_many/belongs_to (one-to-many) in add()? i’m looking at the add() code and it looks like it only handles has_and_belongs_to_many. the old ORM::add() checks for 3 different kinds of relationships. i don’t see any similar code in the new ORM.
August 1st, 2008 at 11:46 am
Quite possible, I haven’t yet looked into it.
August 22nd, 2008 at 6:08 pm
Minor correction:
where(’email’=>’info@example.com’)->find_all()
is supposed to be
where(array(’email’=>’info@example.com’))->find_all()