我有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的对象无法转换为字符串。
需要修复什么?
答案 0 :(得分:1)
您有代码错误而不是
$objects = $this->$em->createNativeQuery($sql, $rsm)->getResult();
仅使用
$objects = $em->createNativeQuery($sql, $rsm)->getResult();