Why var_dump a Doctrine Objects kill my Apache?
Question
I have a very strange problem, when I try to var_dump
(or print_r
) a Doctrine Object, my Apache responses with an empty blank page (200 OK header). I can var_dump
a normal php var like:
$dummy = array("a" => 1, "b" =>2);
And it works fine. But I can't with any object from any Doctrine class, (like a result from $connection->query()
, or an instance of a class from my object model with Doctrine).
Anybody knows why this happens?
Solution
I've had that sometimes when trying to print_r()
a self-referencing object - it gets into a loop and runs out of memory. Possibly that's what's happening to you.
Try increasing the memory limit (ini_set('memory_limit', '256M');
) and see if that fixes it.
Edit: I don't think there's an actual fix for this - it's PHP's internal var_dump
/ print_r
that don't limit depth on recursion (or don't do it properly, at least). If you install the XDebug extension, this can replace the built-in var_dump
with a version that handles recursion much better.
OTHER TIPS
Lazy load proxies always contain an instance of Doctrine’s EntityManager and all its dependencies.
Therefore a var_dump
will possibly dump a very large recursive structure which is impossible to render and read. You have to use \Doctrine\Common\Util\Debug::dump()
to restrict the dumping to a human readable level. Note that the default depth for this function is set to 2 (it's the second parameter)
Use the toArray
method of the Doctrine_Record
class
var_dump($doctrine_record->toArray());
will only display the DB fields and avoid dumping the complete Doctrine internals (which contains self reference/recursion btw)
You can use toArray if you are sure the object is an instance of Doctrine_Collection. Xdebug does not help with doctrine records.
The way I suggest is implementing a custom recursive function to print object, that use Doctrine_Record::toArray() when neeeded
function var_dump_improved()
{
foreach (func_get_args() as $arg) {
if ($args instanceof Doctrine_Collection) {
print_r($arg);
} else if ( $arg instanceof Traversable || is_array($arg) ) {
// do a foreach and recall var_dump_improved on subelements
} else if (...) {
// other types
}
}
}
Some recursive function to debug with max nesting levels are here
http://php.net/manual/en/function.var-dump.php
Look at the comments, look for "recursion"