Mysql
 sql >> Base de Dados >  >> RDS >> Mysql

Usando createNativeQuery para unir duas entidades sem chave estrangeira


Acho que você precisa adicionar explicitamente id na cláusula SELECT:
SELECT s.id, s.subcategory_id, s.type, IF(s.type = 2, c2.name, c3.name) as name, c2.id as c2id, c3.id as c3id
    FROM show_subcategory s
    LEFT JOIN category2 c2 ON (s.type = 2 AND s.subcategory_id = c2.id)
    LEFT JOIN category3 c3 ON (s.type = 3 AND s.subcategory_id = c3.id)
    WHERE s.category1_id = ? ORDER BY s.order_list

E adapte o ResultSet da seguinte forma:
$rsm = new ResultSetMapping;
$rsm->addEntityResult('Acme\CoreBundle\Entity\ShowSubcategory', 's');
$rsm->addFieldResult('s', 'id', 'id');
$rsm->addFieldResult('s', 'subcategory_id', 'subcategoryId');
$rsm->addFieldResult('s', 'type', 'type');
$rsm->addFieldResult('s', 'order_list', 'orderList');

$rsm->addJoinedEntityResult('Acme\CoreBundle\Entity\Category2' , 'c2', 's', 'category2');
$rsm->addFieldResult('c2', 'c2id', 'id');
$rsm->addFieldResult('c2', 'name', 'name');

$rsm->addJoinedEntityResult('Acme\CoreBundle\Entity\Category3' , 'c3', 's', 'category3');
$rsm->addFieldResult('c3', 'c3id', 'id');
$rsm->addFieldResult('c3', 'name', 'name');