Scott’s Technical Blog

Tag: object

Where’s My Doctrine Object?

by scott on Jun.23, 2009, under doctrine, symfony

The Doctrine ORM does a nice job of handling object oriented data, but has a number of flaws when it comes to finding the objects again. Every object it creates is stored in the Table’s repository, but they are not indexed in any meaningful way. It’s pretty much up to you to keep track of your objects once you create them.

What you’d like to do is check to see if a particular object has already been instantiated and not do it again. Recreating the same object multiple times is costly in terms of CPU time even if you use SQL caching.

When it comes to keeping objects around, Doctine is kind of half-pregnant. They are definately there and Doctrine does a good job of making sure you never have duplicate objects floating around that represent the same data in the database. However, finding them is another story.

For example, if you want to see if the User with ID=4 has an object floating around in memory, you can do this:

$repository = Doctrine::getTable(’User’)->getRepository();
foreach ($repository as $u) {
if ($u->getId() == 4) {
do something
}
}

This is great, but imagine if the you have a lot of User objects floating around. This is a very slow way of finding one of them.

What we’d really like is for the repository to be indexed in some meaningful way (or ways), but it’s indexed by a sequential number that’s incremented each time a Doctrine_Record is created. You can get this reasonably useless number by calling $obj->getOID().

If you’ve read a group of objects using the foriegn key or local key relationships of an object, there is another way of finding your objects. All of the references that have been read are stored in an array of reference Doctrine_Collections. To see all of the references for a user object, you can do this:

foreach ($user->getReferences() as $reference)
foreach ($reference as $obj)
(do something with $obj)

You can also get a single reference array using

$collection = $user->reference(’PhoneNumbers’)

where PhoneNumbers is the name of an object set referenced by $user.

These are some basic functions you might find useful. I’ll post later on some of the deficiencies I’ve found in Doctrine relating to handling object associations where the many-to-many is MANY-to-MANY. Doctine is too eager to load EVERYTHING into memory which is find for a few objects, but not so good when it ends up being 10,000.

Leave a Comment :, , more...

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit our friends!

A few highly recommended friends...

Archives

All entries, chronologically...