sql >> Database >  >> RDS >> Mysql

CreateNativeQuery gebruiken om twee entiteiten samen te voegen zonder refererende sleutel

Ik denk dat je expliciet id moet toevoegen in de SELECT-clausule :

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

En pas de ResultSet als volgt aan:

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



  1. Microsoft Access Table Tips – Trucs &Richtlijnen Deel 2

  2. Windows Batch Script om lokale MySQL-databases te back-uppen en alleen N laatste FOLDERS met back-upbestanden te bewaren

  3. Gegevens invoegen in MySql DB en weergeven of het invoegen is gelukt of mislukt

  4. 1-1 Chatsysteem met PHP/MySQL