Symfony2, Doctrine2, left join (dql) and its result
Asked Answered
L

0

0

So we have such dql

    $oQuery = $this->createQueryBuilder('assets')
        ->addSelect('flags')
        ->addSelect('types')
        ->addSelect('groups')
        ->leftJoin('Site\MainBundle\Entity\123\invFlags', 'flags', Join::WITH, 'assets.flag = flags.flagID')
        ->leftJoin('Site\MainBundle\Entity\123\invTypes', 'types', Join::WITH, 'assets.typeID = types.typeID')
        ->leftJoin('Site\MainBundle\Entity\123\invGroups', 'groups', Join::WITH, 'types.groupID = groups.groupID');

    if (!empty($aFilter)) {
        $bFirst = true;

        foreach ($aFilter as $sKey => $sValue) {

            if ($bFirst) {
                $oQuery->where($oQuery->expr()->eq('assets.' . $sKey, $sValue));
                $bFirst = false;
                continue;
            } else {
                if ($sValue === null) {
                    $oQuery->andWhere($oQuery->expr()->isNull('assets.' . $sKey));
                }
            }
        }
    }

    return $oQuery->getQuery()->getResult();

We call it from repository and have result... BUT. In sql query ($oQuery->getQuery()->getSQL()) I see all columns and proper left join I need, but NOT in result. Why it happens? When I return '->getScalarResult()' I receive all needed data but like array and with prefix of tables

array(38) {
  ["assets_id"]=>string(4) "4558"
  ["assets_characterID"]=>string(8) "90206263"
  ["assets_parentItemID"]=>NULL

Can I anyhow get simple 'combined' object' with all get methods?

ADD: Forgot to say that there are no relations in entities.

Lacey answered 21/11, 2013 at 23:22 Comment(2)
Then add the relations. Your join works because you are explicitly setting the join relations. But that doesn't help Symfony with the mapping.Ferne
My db is really huge. Entities will be a trouble. But I will think in this way. Thnx.Lacey

© 2022 - 2024 — McMap. All rights reserved.