【问题标题】:PHP/Symfony2 - print_r on ObjectPHP/Symfony2 - 对象上的 print_r
【发布时间】:2015-04-20 16:33:02
【问题描述】:

我有一些奇怪的事情正在发生,我想知道它们是否与 PHP 或 Symfony 相关。基本上,从我的表单发出一个 ajax 请求并将表单数据发布到我的控制器。

现在我需要对刚刚保存到数据库的实体做一些工作,所以为了让它到达它需要去的地方,我有一个 EventListener 并且我做了

protected  $alertEntity;

public function postPersist(LifecycleEventArgs $args)
{
    $entity = $args->getEntity();
    if ($entity instanceof AvailabilityAlert) {
        $this->alertEntity = $entity;
    }
}

public function postFlush(PostFlushEventArgs $args)
{
    $this->api_service->addFlightsAction($this->alertEntity);
}

所以我在 postPersist 中获取实体,然后将其传递到 postFlush 中它需要去的地方。

这一切都很好,这里没有问题。奇怪的是这个

public function addFlightsAction($alert){
    print_r($alert);
}

如果我在 print_r 中有上述内容,它将永远持续下去,直到最终导致错误分配大小溢出。

如果我在 var_dump 中执行上述操作,它会为我打印出警报,但它的格式不可读,例如(简短摘录)

<pre class='xdebug-var-dump' dir='ltr'>
<b>object</b>(<i>Ontro\AlertBundle\Entity\AvailabilityAlert</i>)[<i>284</i>]
  <i>private</i> 'id' <font color='#888a85'>=&gt;</font> <small>int</small> <font color='#4e9a06'>39</font>
  <i>private</i> 'searchCommand' <font color='#888a85'>=&gt;</font> <small>string</small> <font color='#cc0000'>'A20APRLONLAX'</font> <i>(length=12)</i>
  <i>private</i> 'isConnecting' <font color='#888a85'>=&gt;</font> <small>string</small> <font color='#cc0000'>'no'</font> <i>(length=2)</i>
  <i>private</i> 'lastUpdated' <font color='#888a85'>=&gt;</font> 
    <b>object</b>(<i>DateTime</i>)[<i>288</i>]
      <i>public</i> 'date' <font color='#888a85'>=&gt;</font> <small>string</small> <font color='#cc0000'>'2015-02-19 10:54:54'</font> <i>(length=19)</i>
      <i>public</i> 'timezone_type' <font color='#888a85'>=&gt;</font> <small>int</small> <font color='#4e9a06'>3</font>
      <i>public</i> 'timezone' <font color='#888a85'>=&gt;</font> <small>string</small> <font color='#cc0000'>'Europe/London'</font> <i>(length=13)</i>
  <i>private</i> 'isDeleted' <font color='#888a85'>=&gt;</font> <small>int</small> <font color='#4e9a06'>0</font>
  <i>private</i> 'alertStatus' <font color='#888a85'>=&gt;</font> <small>string</small> <font color='#cc0000'>'Active'</font> <i>(length=6)</i>
  <i>private</i> 'bookingClass' <font color='#888a85'>=&gt;</font> 
    <b>object</b>(<i>Doctrine\ORM\PersistentCollection</i>)[<i>372</i>]
      <i>private</i> 'snapshot' <font color='#888a85'>=&gt;</font> 

那么它为什么要这样做呢?有什么办法可以很好地打印出来让我看到吗?我不介意上面的输出,只要它去掉了所有的 html 标签。

谢谢

【问题讨论】:

    标签: php ajax symfony


    【解决方案1】:

    您已经安装了 xdebug ,它为您设置了 var_dump 格式。要么按照说明 here 禁用它,但这仍然会给您带来与 print_r 相同的问题,并出现分配错误。

    如果你想保留 xdebug(我想你应该这样做,并且应该这样做),你有两个选择,要么在你的 php.ini 中禁用 xdebug.overload_var_dump (docs),要么你可以改用 \Doctrine\Common\Util\Debug::dump($alert)

    在 Symfony 2.6 中,他们引入了一个新功能,VarDumper,这是一个非常强大的内置调试工具。您可以在此处查看文档:http://symfony.com/doc/current/components/var_dumper/introduction.html

    【讨论】:

    猜你喜欢
    • 2013-11-03
    • 1970-01-01
    • 2011-09-06
    • 2015-10-01
    • 2016-11-22
    • 2011-03-25
    • 1970-01-01
    • 2015-11-14
    • 1970-01-01
    相关资源
    最近更新 更多