I have 2 non related entities: External and Internal. I need to union select all results from entities. I am using ResultSetMapping and Native SQL to do this:
$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();
I keep getting this error: Catchable Fatal Error: Object of class Doctrine\ORM\EntityManager could not be converted to string.
What needs to be fixed?
You have a code error instead of
$objects = $this->$em->createNativeQuery($sql, $rsm)->getResult();
use just
$objects = $em->createNativeQuery($sql, $rsm)->getResult();