【问题标题】:Catchable fatal error: Object of class DoctrineORMModule\Proxy\__CG_可捕获的致命错误:DoctrineORMModule\Proxy\__CG_ 类的对象
【发布时间】:2015-04-18 23:57:41
【问题描述】:

我尝试从查询中获取结果

$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(); ?>

但我有一个错误

可捕获的致命错误:类 DoctrineORMModule\Proxy__CG__\ApanelUsers\Entity\Userstatus 的对象无法转换为 /home/xtadmin/localhost/panorama-hotel.local/www/module/ApanelUsers/view/apanel-users 中的字符串/index/index.phtml 第 27 行

【问题讨论】:

  • 能否添加\ApanelUsers\Entity\Userstatus的定义。我不确定,但您可能引用了错误的列。 referencedColumnName 应该是外键指向的任何内容(通常是 id 列)。

标签: php doctrine-orm zend-framework2


【解决方案1】:

错误很明显,您无法回显必须调用其方法之一的整个关联实体,当您调用 $user-&gt;getUserStatusid() 时,这意味着它将返回您的整个 ApanelUsers\Entity\Userstatus 实体对象,因此您无法回显整个实体立即使用&lt;?= entity ?&gt;

您可以在 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();
    }

?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-08
    • 2013-08-07
    • 1970-01-01
    • 2020-07-28
    • 2018-03-12
    相关资源
    最近更新 更多