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.

Mirage
  • 30,868
  • 62
  • 166
  • 261
  • What browser are we talking about? – Madara's Ghost Aug 10 '12 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 Aug 10 '12 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 Aug 10 '12 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 Aug 10 '12 at 13:28
  • I have a bare-bones class and my browser crashed too. I'm hating all these retarded defaults. – ChocoDeveloper Sep 01 '12 at 09:22
  • This is also relevant for ZF2! – NiMeDia Dec 12 '13 at 10:30
  • xdebug is definitely what you need, yes! – barbieswimcrew Mar 14 '16 at 22:09

9 Answers9

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.

Pete
  • 1,305
  • 1
  • 12
  • 36
mgiagnoni
  • 3,454
  • 1
  • 20
  • 9
  • 6
    You can also `dump()` with **MaxDepth**, in `dump()` second argument is **MaxDepth**. – M.B Kakadiya Dec 02 '13 at 09: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. – Andrea Sprega Jan 18 '14 at 13:26
  • This function is very helpful! Saved me from browser crashes as well. – Ren Aug 22 '16 at 10:33
  • This class is deprecated - you should use the [Symfony var dumper](https://symfony.com/doc/current/components/var_dumper.html) instead – Mathias Bader Sep 06 '22 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));
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

goto
  • 7,908
  • 10
  • 48
  • 58
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.

Rad80
  • 823
  • 7
  • 17
2

The get_object_vars() improve the visualization too.

echo "<pre>";
\Doctrine\Common\Util\Debug::dump(get_object_vars($user));
Wellington Lorindo
  • 2,405
  • 19
  • 21
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();
J-who
  • 550
  • 5
  • 9
2

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

1

Just use echo serialize($user);

AlexM
  • 317
  • 2
  • 5