【问题标题】:Symfony2 Error: Object of class Doctrine\ORM\EntityManager could not be converted to stringSymfony2 错误:Doctrine\ORM\EntityManager 类的对象无法转换为字符串
【发布时间】:2016-01-11 01:30:31
【问题描述】:

我有 2 个不相关的实体:外部和内部。我需要联合选择来自实体的所有结果。我正在使用 ResultSetMapping 和 Native SQL 来执行此操作:

 $em = $this->getDoctrine()
        ->getManager()->getConnection();
 $rsm = new \Doctrine\ORM\Query\ResultSetMapping();
    $rsm->addEntityResult('ExternalBundle:External', 'e');
    $rsm->addFieldResult('e', 'id', 'id');
    $rsm->addFieldResult('e', 'status', 'status');
    $rsm->addFieldResult('e', 'name', 'name');
    $rsm->addEntityResult('InternalBundle:Internal', 'i');
    $rsm->addFieldResult('i', 'id', 'id');
    $rsm->addFieldResult('i', 'status', 'status');
    $rsm->addFieldResult('i', 'name', 'name');
    $sql = "SELECT e.*
FROM external_print e
UNION
SELECT i.*
FROM internal_print i";
    $objects = $this->$em->createNativeQuery($sql, $rsm)->getResult();

我不断收到此错误:可捕获的致命错误:Doctrine\ORM\EntityManager 类的对象无法转换为字符串。

需要解决什么问题?

【问题讨论】:

  • 从 $em 中移除 $: $this->em
  • @viktor77 现在显示注意:未定义属性:em
  • 试试这个:$em = $this->container->get('doctrine')->getManager();

标签: symfony doctrine-orm doctrine native-sql sqlresultsetmapping


【解决方案1】:

你有一个代码错误而不是

$objects = $this->$em->createNativeQuery($sql, $rsm)->getResult();

只使用

$objects = $em->createNativeQuery($sql, $rsm)->getResult();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多