[nycphp-talk] Database Abstraction / ORM
Rob Marscher
rmarscher at beaffinitive.com
Mon May 7 10:40:21 EDT 2012
I use the Lithium php framework... so I'll include some notes for your
requirements as it relates to that framework.
On Mon, May 7, 2012 at 8:16 AM, Justin Demaris <justin.demaris at gmail.com>wrote:
> 1) When I instantiate an object by it's ID multiple times, it doesn't
> bother to hit up the database after the first time, but just keeps giving
> me copies of the same object
>
I would imagine that most db abstractions won't assume you want to do this
and you'll need to implement it in your application.
In Lithium, your config/bootstrap/cache.php file, you would register a
filter around the Model::find method:
Filters::apply('lithium\action\Dispatcher', 'find', function($self, $params,
$chain) {
// examine the arguments to generate a cache key
// Lithium cache has a "memory" adapter that can be used
// if object found in cache, return
// could also check for a param `$params['cache'] === false` to
// support skipping the cache
$data = $chain->next($self, $params, $chain);
// add storing the data to the cache
return $data;
});
2) Lazy load the object values. There are a number of patterns where I've
> seen people instantiate a bunch of objects and then only use a small subset
> of them. It would be nice if the object only loaded the data when we try to
> reference one of its non-ID properties.
With Lithium and MongoDB (I'm not sure about other dbs), the result of the
database call for a list is just a MongoCursor and the "entities" (as
Lithium calls them) are only instantiated when needed. It does cache those
objects inside the result object. It is possible to unset that cached
object though and free up memory (I was able to roll out a huge CSV file
this way without using much memory).
3) Ability to tweak the back end to work with other database systems
> (especially Riak, Mongo and Cassandra)
MongoDB support is strong and built-in to Lithium. It's not too difficult
to roll out adapters for other databases - especially those that operate
over http. There is a CouchDb adapter included that works this way.
http://lithify.me
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.nyphp.org/pipermail/talk/attachments/20120507/85a52e1c/attachment.html>
More information about the talk
mailing list