
Just a quick update on a little project I’ve been working on over the past few months. Idiorm is “a lightweight nearly-zero-configuration object-relational mapper and fluent query builder for PHP5,” and is released under a BSD license.
The starting point for Idiorm was the topic of my talk at BarCampBrighton last year: “Minimalism in Web Development”, the idea that for many purposes, simpler solutions are often preferable to complex ones.
Idiorm lets you start using an ORM in your application with almost no effort. No XML files, model classes, database introspection or code generation is required: You just need to tell the ORM how to connect to your database, and away you go.
The fluent interface allows you to build most basic queries without writing any SQL, and gets out of the way when you need to do something more complex (both raw WHERE clauses and complete raw queries are supported). Once you have one or more instances of the ORM class (representing the rows in your database tables) you can access the data in them directly as properties of the objects (using PHP’s magic __get and __set methods internally), modify and save them, as well as delete them. For full documentation and lots of code samples, see the README file.
Obviously, such a simple project (the whole ORM class is only about 600 lines of code) would probably never be used in a huge, complex PHP application. The main drawback to using a generic ORM class instead of domain-specific model classes is that you can’t attach your own behaviour to the models. So the (extremely sensible) idea of placing most of your business logic in your models (Skinny Controller, Fat Model) isn’t possible with Idiorm as-is.
I have three answers to this problem. The first is that many applications simply don’t need any business logic more complex than basic CRUD operations, for which Idiorm is perfect. Secondly, one clear use case for Idiorm is to clean up legacy applications which are littered with raw SQL statements, usually passed directly into some custom-written “database abstraction” class or set of functions. Here, the application is already a mess, and in the absence of a complete rewrite, integrating Idiorm can only make things better. Finally, there’s no reason why you couldn’t build a full model layer as a lightweight wrapper on top of Idiorm. This would probably take the form of a factory object which returns instances of the correct model class, using Idiorm to populate them with data. I have a rough-and-ready implementation of this idea on my laptop which I may release at some point, but it’s easy enough to build yourself. If you’re using PHP 5.3 (and so have access to late static binding), you could even code up a pretty nice implementation of the active record pattern around Idiorm.
So to summarise, Idiorm isn’t really intended to replace something like Doctrine or Propel (or ActiveRecord, or Django’s ORM, or SQLAlchemy, etc). It’s designed to be a quick and easy solution to database abstraction in small-to-medium sized applications. And let’s face it – most of us are building small-to-medium sized applications. We’re building bicycles, not space shuttles. If Doctrine is a rocket engine, Idiorm is a bottom bracket. But if all you want to do is get to the corner shop, a bicycle is a much quicker way to travel.
Finally, if you think you’d find Idiorm useful, please let me know. Fork the project, make suggestions using GitHub’s Issues system or just leave a comment on this post.


Hey, I found your ORM through an answer on StackOverflow.
I have to say, I really like the simplicity this provides for basic ORM stuff.
I’ve looked high and low for a simple and easy-to-use ORM, and this is it.
Thanks a ton for the great work!
I also found this via StackOverflow.
I’ve been using Idiorm for a couple months and it’s an absolute delight. I like the way you think Jamie. Your code is clean, well commented and your documentation is top notch – needless to say I love this ORM. It fits nicely with a micro
MVC framework I wrote but haven’t released yet (only ~200 LOC without comments).Anyway, props Jamie! Nice work!
Thanks both – I really appreciate the comments, and it’s great to know that Idiorm is getting used in the real world. As ever, I welcome any comments or suggestions on how it could be improved.
@shane – let me know if you release your framework!
Is there any chance that you’d convert it to JavaScript for use with node.js? Great work, sir!
Hi Colin,
Rewriting for Node would be tricky, for two reasons. As far as I know there’s no equivalent to PDO – a common interface to multiple database backends – so this abstraction would have to be written first (or built into Idiorm, but this would probably not be the right approach). Also, the API would look fairly different because of the need to be asynchronous. The find_one and find_many methods would probably have to take callback functions to run when the results are returned from the database.
I’ll bear it in mind as something to think about for the future. Thanks for the comment!
Thank you for idiorm !! Nice name !!
Thanks
I’ve found you page via stackoverflow
Hi, I do really like the ‘minimalism in web development’ philosophy. Nevertheless, I tried to understand and use Idiorm for a day now. I’d I had some questions about the possibilities to create Value Objects with it (something that would be similar to EJB in j2ee). What should I do if I’ve got a one2many relation in a db? I’d like to have a php object that would have as a property, an array with the values of the related table in it. Does your code makes that possible? I can work on the code to make that possible, but I can’t figure out (altough it’s REALLY well commented) where to implement this.
Would that functionnality be against your views about minimalism?
Hi Greg,
Idiorm doesn’t currently support any relations. This is partly by design (to keep the code simple) but I’m not completely against the idea – I just haven’t thought of a suitable API yet that won’t overcomplicate the configuration, and won’t require overly verbose method calls.
At the moment, the easiest way to achieve what you’re trying to do is simply to manually run two queries:
$main_object = ORM::for_table(’first_table’)->find_one($id);
$related_objects = ORM::for_table(’related_table’)->where(’foreign_key’, $main_object->id())->find_many();
I will keep thinking about the problem and see if I can come up with a way of adding this to Idiorm.
Thanks for the comment!
Thanks to you for the response!
I think I’ll use the methods you describe inside a DAO that will fill the properties of a value object. That class i’d create will have the other relations as properties. As far as abstraction is concerned, I don’t think it’s the good way to do it (nor it is not to repeat myself) but I do really like Idiorm and I want to include it in my project.
Thanks again!
[...] Idiorm A lightweight nearly-zero-configuration object-relational mapper and fluent query builder for PHP5. [...]
i still dont understand how to view data from multiple row (find many)
Hi Jim,
Can you explain the problem you’re having in more detail?
Jamie
i want to select 2 row in table using
$people = ORM::for_table(’table01′)->limit(2)->find_many();
and if i print_r($people); it comes with series of array, how can i echo it using get()?
$peoplewill just be a normal PHP array.You can get each member like this:
$person_1 = $people[0];
$person_2 = $people[1];
or you can loop over them
foreach ($people as $person) {
echo $person->name;
}
Does that help?
ok thankyou