Ok ignoring query performance, with AR, I can do unlimited chaining:
For example:
PHP Code:
$user = $this->data->createRecord('User', $id);
echo $user->orders[0]->products[0]->manufacturer->address->country;
Which produces readable, concise code.
Whether this is doing joins, or feching each record one at a time is irrelevant.
How do I achieve the same with DataMapper?
If it was:
PHP Code:
$user = $this->userMapper->getById($id);
echo $user->orders[0]->products[0]->manufacturer->address->country;
This means the mapper, when it does the query has to fetch pretty much the entire database into the user object. It needs all the users orders, all the products related to all the users orders, all the manufacturers associated with those products, etc.
Obviously that's feasible in the real world and would eat memory fast.
It seems to me the only way to do this with DataMapper is:
PHP Code:
$user = $this->userMapper->getById($id);
$order = $this->orderMapper->getByUser($user->id)[0];
$product = $this->productMapper->getByOrder($order->id)[0];
$manufacturer = $this->manufacturerMapper->getById($product->manufacturerId);
$address = $this->manufacturerAddressMapper->getById($manufacturer->addressId);
echo $address->country;
Is this correct or am I missing something?
That produces horrible verbose code, which is clearly a OO interface to a relational back end.
Thanks
Bookmarks