How do I extend this simple DataMapper?
Can someone please deduce a specific example from the following:
http://www.urdalen.com/blog/?p=210
.. shows how to deal with relationships one-to-many
and many-to-many
?
I sent an email to the author some time ago but received no response. I love his idea, but can't figure out how to implement it outside of a simple one-table relationship.
Note. I don't want to use a full blown ORM. I like doing my SQL by hand. However, I would like to improve the design of my application code. Now every domain object has its own class, filled with requests wrapped in static methods. They just return a scalar, 1d array (record) or 2d array (recordset) depending on the request.
The problem with ORM (Impedance Resistance, as it is called) is with relationships. In an object graph (in-memory objects), relationships are pointers to other objects. In a relational database, relationships are reversed; This makes a simple mapping between the two models impossible, which is why ORMs are so complex.
If you want to stay close to the database (avoiding the ORM), you shouldn't try to take your mind off the relationship. The way I write datamappers is something like this:
$car42 = $car_gateway->fetch(42);
$wheels = $wheel_gateway->selectByCar($car42);
As opposed to the ORM way:
$car42 = $car_gateway->fetch(42);
$wheels = $car42->selectWheels();
This means the gateways get into your client code, but it also makes things very transparent and database-like.
source to share
If you're looking for a simple and portable ORM DataMapper check out phpDataMapper . It is only PHP5 and PDO dependencies and it is very small and lightweight. It supports table relationships and some other very nice features.
source to share
Given your answer to Tom's answer, I would recommend that you take a look at something like Zend Framework. Its ORM uses it or leaves it with an architecture that can be implemented in stages.
When I went to my current employer, they had an app that had just been completed a few months ago, but had gone through one or two previous versions, and the current version had been in development six months longer than expected. However, the code base was messy. For example, there was no abstraction between database access logic and business logic. And they wanted me to move the site forward, creating new features, extending existing features, and fixing existing bugs in the code. To further complicate matters, they did not use any form of sanitation for data inputs or outputs.
As soon as I started to delve into the problem, I realized that I needed a solution to abstract problems that can be implemented in stages, because they obviously were not going to be completely rewritten. My initial approach was to write custom ORMs and DALs that will do the heavy lifting for me. It worked great because it didn't intrude into the existing codebase, and therefore it allowed me to subtly move entire parts of the application into the new architecture.
However, after putting most of our site's user area into this new framework and building the whole application on my custom platform (which also included the UI controller and mvc implementation) I move to Zend Framework (that's my choice, although I'm sure some others structures will also work in this situation).
When switching to Zend Framework, I have absolutely no worries about the legacy code base because:
- I can build new models and refactor old models (built on my regular framework) unobtrusively.
- I can refactor existing controllers (such as these) to be wrapped in a class that behaves according to Zend's MVC so that it becomes a side issue in order to actually start using Zend's Front-End Controller.
- Our views are already built by Smarty, so I don't have to worry about decoupling the controller and presentation logic, but I can extend the Zend Framework so that I can render existing templates in Smarty when creating new templates in straight PHP.
Basically, Zend Framework uses it or leaves the architecture, which makes it a joy to use in existing projects, because new code and refactoring code should not invade existing code.
source to share