Hibernate for PHP?

I have come up with something that can, in no way, be compared to Doctrine or Propel. It does, however seem to work pretty well for me. It was built with ease of use in mind. I feel that things in PHP should take a minimum of lines or the interface may be cumbersome. I will show a couple of its paradigms and you may possibly find it interesting.


// Initialize using the built-in DBA layer (I needed to run this in a non-PDO environment)
// AFAIK, you could just as easily use FAPersistence::connect(new PDO('...'));
$orm = FAPersistence::init('mysql://user:pass@host/database');

// Find a user the suggested way
$user = $orm->User(4);

// Otherwise, the long way
$user = new User(array('id' => 4));
$user->find();

// Note that if both ways were used on the same page request, they would nevertheless contain references to a common record object.

// Find all users
$users = $orm->findAll('User');

// Find users who joined in the last day
$users = $orm->findAll('User')->where('User.joined > CURDATE()-1'); // I'm not sure about the SQL, but that's not the point

// Find all users ordered by their full name
$users = $orm->findAll('User')->orderBy('User.fullname');

// Method chaining for simple updates
$orm->User(4)->set('fullname', 'Real Name')->save();

The entities returned through these queries all implement __get and __set magic methods (not shown above).

Thoughts?

I’ve been working in a project like hibernate for PHP. Been 3 years since i began and right now im having some problems with the language.
Since it cant “runtime extend” a class, its kinda hard to implement lazy fetch proxying if you are using type hinting. Unless your server suports runkit allowing best AoP in PHP. There are other issues as well but it runs fine and after it became stable (documented in my home language Brazillian Portuguese, tho) never needed to declare SQL statements anymore, but i realize that it cant do complex reports yet. If you are interested i can provide the code and later on translated docs.

Interfaces?

even using interfaces… i assume you’ll hint your parameter with the class it expects. if its a interface you’ll need a proxy implementing it or make your classes implement an fw interface. I do the trick using annotations (phpDocComment parsed with regex and extending the reflection classes to suport them) and i tried hard to not force inheritance or interface implementations… seems that i have to face it cant be done like i want right now. Dont want to ask for hinting removal also…

The point of interfaces is pretty much that you typehint to the interface, not the concrete class. You would be forcing the user to write code this way, yes, but that wouldn’t be too bad.

I was used to do it using the concrete class since its a “bean”. Thanks for your advice.
Right now im implementing inheritance using interface (for multiple subclass support) and im thinking about using the discriminator like hibernate. it already supports one table per class and single table for entire object (no discriminator yet). Next is the basic validation using annotations like Size, NotNull etc…

Can someone educate me as to the pros/cons of the domain model and DAL being tightly/loosely coupled?

In a nutshell, having a domain layer means your code is 1. testable and 2. reusable. If your domain objects don’t rely on the application layer or persistence layer, then they should work equally well in a test framework as in a web application. They should also be simple to reuse in other applications, since they have minimal dependencies and only have single responsibilities.

Thanks for posting this. Interesting indeed. And it would complement a project I’m working on perfectly. It’s refreshing to see this kind of out-of-the-box thinking on occasion.