Catchable fatal error: Object of class DoctrineORMModule\Proxy\__CG_

Catchable fatal error: Object of class DoctrineORMModule\Proxy\__CG_

我尝试从查询中获取结果

$user = $this->getEntityManager()
                     ->getRepository('\ApanelUsers\Entity\Usercommon')
                     ->findAll();

但是如果我尝试添加到查询连接列,我会出错,没有它我会得到很好的结果。 这是我需要的 JoinColumt:

/**
 * @var \ApanelUsers\Entity\Userstatus
 *
 * @ORM\ManyToOne(targetEntity="ApanelUsers\Entity\Userstatus")
 * @ORM\JoinColumn(name="User_StatusId", referencedColumnName="UserStatusId", nullable=false)
 */
private $userStatusid;

/**
 * Set userStatusid
 *
 * @param \ApanelUsers\Entity\Userstatus $userStatusid
 * @return Usercommon
 */
public function setUserStatusid(\ApanelUsers\Entity\Userstatus $userStatusid = null)
{
    $this->userStatusid = $userStatusid;

    return $this;
}

/**
 * Get userStatusid
 *
 * @return \ApanelUsers\Entity\Userstatus 
 */
public function getUserStatusid()
{
    return $this->userStatusid;
}

这是一个视图

<?= $user->getUserStatusid(); ?>

但是我有一个错误

Catchable fatal error: Object of class DoctrineORMModule\Proxy__CG__\ApanelUsers\Entity\Userstatus could not be converted to string in /home/xtadmin/localhost/panorama-hotel.local/www/module/ApanelUsers/view/apanel-users/index/index.phtml on line 27

错误很明显,您无法回显整个关联实体,您必须调用其方法之一,当您调用 $user->getUserStatusid() 时,这意味着它将 return 您 [=14= 的整个对象] 实体,因此您不能使用 <?= entity ?>

一次回显整个实体

您可以在 ApanelUsers\Entity\Userstatus 实体

中实现 __toString() 方法的一种方法
class Userstatus{

     public fundtion __toString(){
     return $this->getTitle() // or you can use any other method to retrurn value
     }
// other properties with their related getters and setters
}

另一种方式是

<?= $user->getUserStatusid()->getTitle(); ?>

但这也会失败,因为在 setUserStatusid() 中您允许状态可以为空,因此对于第二个选项,您需要首先检查它是否是 ApanelUsers\Entity\Userstatus 的实例,然后调用其相关的 getter

<?php

    $status = $user->getUserStatusid();
    if($status instanceof \ApanelUsers\Entity\Userstatus){
    echo $status->getTitle();
    }

?>