简体   繁体   English

symfony2 学说 2 中的 var_dump 数据过多

[英]Too much data with var_dump in symfony2 doctrine2

I have around 40 entities and many bidirectional relationships.我有大约 40 个实体和许多双向关系。 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.每当我使用 var_dump($user) 或任何实体时,我的浏览器都会加载过多的数组和变量数据,然后它就会崩溃。

i want to whats the problem.我想知道有什么问题。

The data is being inserted fine.数据插入正常。 Can i cause issue in production.我可以在生产中引起问题吗?

Replace var_dump() with the debug method dump() provided by Doctrine Common .var_dump()替换为Doctrine Common提供的调试方法dump()

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

It works for single objects and Doctrine collections and should prevent browser displaying issues you are having.它适用于单个对象和 Doctrine 集合,并应防止浏览器显示您遇到的问题。

well formatted :格式正确:

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

简单易行的例子。

var_dump(serialize($Object));

问题在于,在双向关系中,两个实体之间都有一个链接,因此在显示 entity1 时 var_dump 还必须打印 entity2 的所有属性,其中包括 entity1 本身给您一个循环。

Symfony < 2.6 Symfony < 2.6

You can use \\Doctrine\\Common\\Util\\Debug::dump($variable, $depth);您可以使用\\Doctrine\\Common\\Util\\Debug::dump($variable, $depth); it displays doctrine output without the proxy information.它显示没有代理信息的学说输出。

Symfony > 2.6 Symfony > 2.6

If you are using symfony 2.6 or more, I strongly advice you to use dump() .如果您使用的是 symfony 2.6 或更高版本,我强烈建议您使用dump() It shows a well formated and colored output, and you can dynamically expend/hide rows.它显示了格式良好的彩色输出,您可以动态扩展/隐藏行。 在此处输入图片说明

The get_object_vars() improve the visualization too. get_object_vars() 也改进了可视化。

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

With Symfony 2.6 you can now just use dump($var) in your controller and {{ dump(var) }} in twig.使用 Symfony 2.6,您现在可以在控制器中使用 dump($var) 并在 twig 中使用 {{ dump(var) }} 。

Make sure to add this to your AppKernal.php file, in the array('dev', 'test') section.确保将此添加到您的 AppKernal.php 文件中的 array('dev', 'test') 部分。

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

use dump($user) and you can see perfect result in Symfony Profiler!使用 dump($user) 你可以在 Symfony Profiler 中看到完美的结果! good luck祝你好运

只需使用 echo serialize($user);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM