1

I have a join query in my "Item" entity repository class:

public function findByParentItem(parentItemCodes, excludedModules) {
    $qb = $this->getEntityManager()->createQueryBuilder();

    $qb->select('i', 'p')
        ->from('Bundle:ItemParent', 'p')
        ->join($this->getClassName(), 'i', Join::WITH, 'i.itemCode = p.itemCode')
        ->where(
            $qb->expr()->andX(
                $qb->expr()->in('p.parentItem', ':parentItem'),
                $qb->expr()->notIn('i.moduleId', ':excludedModules')
            )
        )
        ->setParameter('parentItem', $parentItemCodes)
        ->setParameter('excludedModules', $excludedModules);

    return $qb->getQuery()->getResult();
}

Item and ParentItem are not connected by association because it might point to another entity as well.

I expect the result to be:

[
    0 => [
        0 => Item,
        1 => ParentItem
    ],
    1 => [
        0 => Item,
        1 => ParentItem
    ],
    ...
]

Instead I get:

[
    0 => Item,
    1 => ParentItem,
    2 => Item,
    3 => ParentItem,
    ...
]

Is this happening because there is no entity association?

EDIT: Changing ->from('Bundle:ParentItem', 'p', 'p.itemCode') gives me:

[
    'ItemCode1' => ParentItem,
    0 => Item,
    'ItemCode2' => ParentItem,
    1 => Item,
    ...
]

If I do:

$qb->select('i', 'p.itemCode as itemCode')

I get this which is more in the lines of what I want:

[
    0 => [
        0 => ParentItem,
        'itemCode' => 'ItemCode1'
    ],
    1 => [
        0 => ParentItem,
        'itemCode' => 'ItemCode2'
    ],
    ...
]

EDIT: My entities:

/**
 * @ORM\Entity(repositoryClass="ItemRepository")
 * @ORM\Table(name="item")
 */
class Item {

    /**
     * @ORM\Column(name="item_code", type="string", length=50)
     * @ORM\Id
     */
    protected $itemCode;

    /**
     * @ORM\Column(name="module_id", type="string", length=10, nullable=true)
     */
    protected $moduleId;

    // .. other properties
}

/**
 * @ORM\Entity(repositoryClass="ItemParentRepository")
 * @ORM\Table(name="item_parent")
 */
class ItemParent {
    /**
     * @ORM\Column(name="item_code", type="string", length=50)
     * @ORM\Id
     */
    protected $itemCode;

    /**
     * @ORM\Column(name="parent_item", type="string", length=50)
     * @ORM\Id
     */
    protected $parentItem;
}
juhah
  • 11
  • 5
  • try with this `from()` function: `->from('Bundle:ItemParent', 'p', 'p.itemCode')` – manix Oct 12 '15 at 13:56
  • It still gives flat array, I edited my question. – juhah Oct 12 '15 at 14:17
  • you should post your entities as well... – Francesco Casula Oct 12 '15 at 14:30
  • I added them, just regular entities without associations. I know I should use associations but for various reasons I don't want to. I would like to understand why the results are formatted the way they are. – juhah Oct 12 '15 at 15:18
  • If you expect an array then retrieve an array with getArrayResult() method. – Alex Oct 12 '15 at 15:19
  • Same problem exists with getArrayResult(), ie. results are "back-to-back" instead of being nested. I'm actually not sure if this even standard behaviour but if selecting a single value with the other entity groups the result set, I would imagine the same being true for selecting two entities? – juhah Oct 12 '15 at 15:22

1 Answers1

0
select(array('item as itemObj', 'parentItem as parentItemObj'))
demongolem
  • 9,474
  • 36
  • 90
  • 105