The combination of Relational Data (database) and Object Oriented Programming (OOP) is not a match made in heaven. The way we work with objects is totally different than the way we access Data. The problem is that in most projects you need to access data and write Object Oriented Classes to display and manipulate that data.
Object-relational mapping (aka ORM, O/RM, and O/R mapping) is a programming technique for converting data between incompatible type systems in relational databases and object-oriented programming languages. This creates, in effect, a “virtual object database” that can be used from within the programming language.
I have come across and worked with two good ORM solutions for PHP:
Open Source Object-relational mapping for PHP
1) CakePHP – has a super easy to use ORM. You just write your schema in the database and corresponding objects, using a naming convention (e.g. ‘Users’ table -> ‘User’ PHP object) and the framework investigates the table structure, using DESCRIBE, and transparently creates the mapping. It supports full CRUD operations and most data relationships. CakePHP is not only an ORM framework; it is a full application framework. This could be an advantage or a disadvantage depending on your willingness to use the other features of CakePHP.
2) Doctrine – a pure, easy to use, ORM solution for PHP. With Doctrine you run a batch process that auto generates PHP objects that represent the data. Doctrine does not perform runtime DESCRIBE or uses naming convention semantics, the generated objects have intimate pre-runtime knowledge about the database schema. Doctrine also supports full CRUD operations and most data relationships.
At the time this is written, neither of these solutions fully supports composite relationships. If you have these kinds of composite keys relationships in your system, you would need to do some SQL and code workarounds. While this is not a major problem, it is something to think about in the initial database schema design.
If you had good/bad experience with any PHP ORM, we would be happy to hear about it.