2015-10-13 80 views
1

我有2個非相關的實體:外部和內部。我需要聯合選擇實體的所有結果。我使用ResultSetMapping和本地SQL做到這一點:Symfony2錯誤:類Doctrine ORM EntityManager的對象無法轉換爲字符串

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

我不斷收到此錯誤:開捕致命錯誤:類學說的對象\ ORM \的EntityManager不能轉換成字符串。

什麼需要解決?

+0

刪除$從$ EM函數:$ this-> EM – viktor77

+0

@ viktor77現在說注意:未定義property:em – user3793667

+0

試試這個:$ em = $ this-> container-> get('doctrine') - > getManager(); –

回答

1

你有一個代碼錯誤,而不是

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

使用只是

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

相關問題