Happyr/Doctrine-Specification

How to Select from Spec::join

Daisuke-sama opened this issue · 1 comments

I have such a code

        $res = $this->getVehicleRepo()->getQueryBuilder(
            Spec::andX(
                Spec::select('model'),
                new IsNotSold(),

                new JoinModelModifier(),
                new ModelsBelongToMaker($makerId)
            )
        );

I'm going to select fields from the Model table, which is joined by new JoinModelModifier(), while all the selections are performed for the root alias, which comes from the ->getVehicleRepo, i.e. the Vehicle table.
Can't understand how to select such a data, except for doing two separate queries on both repos.

By the way with the classic doctrine I can achieve the desirable with

        $res = $this->getNotSoldVehicleQueryBuilder()
            ->select('(v.model)')
            ->addSelect('m.name')
            ->addSelect('m.id')
            ->join('v.model', 'm')
            ->join('m.maker', 'mk')
            ->andWhere('mk.id = :maker_id')
            ->setParameter('maker_id', $makerId)
            ->groupBy('v.model');

You need to use the field operand and give it a alias of the joined entity.

$qb = $this->getVehicleRepo()->getQueryBuilder(
    Spec::andX(
        Spec::select('model'),
        Spec::addSelect(Spec::field('name', 'm'), Spec::field('id', 'm')),
        new IsNotSold(),
        new JoinModelModifier('m', 'mk'),
        new ModelsBelongToMaker($makerId)
    )
);