开发者

Doctrine objects are HUGE

I'm a .NET convert to PHP and am so far having a good time with the transition. I'm using doctrine 1.2 as my ORM and have my models working and everything is connected fine. However, the problem i'm looking at now is the output objects are enormous. I have a fairly simple table called USERS -- it has probably 8 columns and FKs to 4 or 5 other tables. I'm using the code below to hydrate my USERS object:

$q = Doctrine_Query::create()
->select('u.*')
->from('USERS u')
->where('u.VANITY_URL = ?',$Url_Frag);

$us开发者_如何转开发ers = $q->execute();

print_r($users);

I see the object hydrated w/ my data so that's good. However, it also comes along with what looks like a bunch of meta data that I obviously don't need. Overall, the object is over 5000+ lines long! I'm sure there's an obvious switch somewhere that basically says "only emit such-and-such data" but I can't find it in the doctrine manual.

Thoughts?


In Doctrine2, there is a dump() method available at:

\Doctrine\Common\Util\Debug::dump($var, $maxDepth)

It does a job similar to print_r and var_dump, but hides all the Doctrine-related data.

Maybe there is something similar for Doctrine 1.x?


Doctrine 1.2 entities object and collections has a method named "toArray". So you can do:

print_r($users->toArray());


You have several options. One is to switch to doctrine2: it has sleek models, without any magic of doctrine1.

The second ones are to change your hydration mode. You cannot really tweak the doctrine model or fatness of an object without changing the doctrine_record logic. So this might work:

$q = Doctrine_Query::create()
->select('u.*')
->from('USERS u')
->where('u.VANITY_URL = ?',$Url_Frag)
->setHydrationMode(Doctrine::HYDRATE_ARRAY);
$users = $q->execute();

Print_r'ing these objects will be hughe, since there are many nested objects in the doctrine class network (some objects have a bit of the "god complex" in them).

More documentation can be found here: http://www.doctrine-project.org/documentation/manual/1_2/pl/data-hydrators:core-hydration-methods


if I'm not mistaking, there are some circle references in the Doctrine1.2 entities, so print_r or var_dump on them is not a good idea. Actually, if you don't have something like Xdebug that limits the recursion depth, you'll never get the output to the browser.

If you are really concerned of memory consumption, use memory_get_usage function to examine the memory footprint before and after hydration.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜