New ORM, overview of changes

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
  • Primary keys can be defined per-model
  • Database can be defined per-model
  • Table names can be defined per-model, and can be plural or singular
  • Table columns can be defined per-model, as long as appropriate meta data is provided. var_export(ORM->table_columns) to see the syntax and copy it to ORM->table_columns
  • Support for ‘as’ i.e. select user as friend -> syntax array(’friend’=>’user’)

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


12 Responses to “New ORM, overview of changes”

  1. cmike Says:

    How to initialise related? Does this depend on has_many_and_belongs_to??

  2. cmike Says:

    OK - I figured it out ($user->roles), but how to add new related?

  3. Howie Says:

    three times slower ? do you mean faster?

  4. dlib Says:

    No, slower. __call() is 3 times slower than some_method()

  5. Sam Clark Says:

    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

  6. codebirth Says:

    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?

  7. Jon Says:

    “First of all the find_(all)_by_? syntax is gone”

    This is kind of a dumb move, to be honest, but okay

  8. dlib Says:

    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

  9. Jon Says:

    dlib - great! Thank you! :)

  10. bunnyhero Says:

    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.

  11. dlib Says:

    Quite possible, I haven’t yet looked into it.

  12. Alberto Says:

    Minor correction:

    where(’email’=>’info@example.com’)->find_all()

    is supposed to be

    where(array(’email’=>’info@example.com’))->find_all()

Leave a Comment