108

I have around 40 entities and many bidirectional relationships. Whenever i use var_dump($user) or any entity my browser gets loaded with too much data of arrays and variables then it just crashed.

i want to whats the problem.

The data is being inserted fine. Can i cause issue in production.

7
  • What browser are we talking about? Commented Aug 10, 2012 at 12:57
  • 3
    Are you using xdebug? If not, consider to use it and instead of var_dump just make use of the step debugger with a IDE like Ecplipse, Netbeans or PHPStorm. All these will display the variables data nicely.
    – hakre
    Commented Aug 10, 2012 at 12:57
  • What do you mean by "crashing" - does the browser application (or tab) close, or it display no result, or the page is interrupted?
    – Yuriy
    Commented Aug 10, 2012 at 12:57
  • my browser displays very long page of variables data with all my entiies and all that. looks like it goes in never ending loop. i tried botf firefox and chrome. if i try any other class whic has no relation then it works ok but with many relationships it freezez the computer. i had to end task that
    – Mirage
    Commented Aug 10, 2012 at 13:28
  • I have a bare-bones class and my browser crashed too. I'm hating all these retarded defaults. Commented Sep 1, 2012 at 9:22

9 Answers 9

233

Replace var_dump() with the debug method dump() provided by Doctrine Common.

\Doctrine\Common\Util\Debug::dump($user);

It works for single objects and Doctrine collections and should prevent browser displaying issues you are having.

4
  • 6
    You can also dump() with MaxDepth, in dump() second argument is MaxDepth. Commented Dec 2, 2013 at 9:26
  • 4
    If you prefer to have the debug output in your php error log, use the following: error_log(print_r(\Doctrine\Common\Util\Debug::export($variable, $depth),1)); It's pretty cumbersome to type every time, but you can easily create a macro for it. Commented Jan 18, 2014 at 13:26
  • This function is very helpful! Saved me from browser crashes as well.
    – Ren
    Commented Aug 22, 2016 at 10:33
  • This class is deprecated - you should use the Symfony var dumper instead Commented Sep 6, 2022 at 18:21
21

well formatted :

echo '<pre>';
\Doctrine\Common\Util\Debug::dump($user, $recurciveLevelToDisplay);
echo '</pre>';
5

Simple and easy example.

var_dump(serialize($Object));
0
5

Symfony < 2.6

You can use \Doctrine\Common\Util\Debug::dump($variable, $depth); it displays doctrine output without the proxy information.

Symfony > 2.6

If you are using symfony 2.6 or more, I strongly advice you to use dump(). It shows a well formated and colored output, and you can dynamically expend/hide rows. enter image description here

4

The problem is that in a bidirectional relationship both entities have a link to each other, so while displaying entity1 var_dump will also have to print all properties of entity2, which include entity1 itself giving you a loop.

1
  • This is the only answer that explain the WHY it happens. Commented Sep 12, 2019 at 15:33
2

The get_object_vars() improve the visualization too.

echo "<pre>";
\Doctrine\Common\Util\Debug::dump(get_object_vars($user));
2

With Symfony 2.6 you can now just use dump($var) in your controller and {{ dump(var) }} in twig.

Make sure to add this to your AppKernal.php file, in the array('dev', 'test') section.

$bundles[] = new Symfony\Bundle\DebugBundle\DebugBundle();
2

use dump($user) and you can see perfect result in Symfony Profiler! good luck

1

Just use echo serialize($user);

Not the answer you're looking for? Browse other questions tagged or ask your own question.