Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to Select from Spec::join #205

Open
Daisuke-sama opened this issue Jul 7, 2019 · 1 comment
Open

How to Select from Spec::join #205

Daisuke-sama opened this issue Jul 7, 2019 · 1 comment
Labels

Comments

@Daisuke-sama
Copy link

Daisuke-sama commented Jul 7, 2019

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');
@peter-gribanov
Copy link
Member

peter-gribanov commented Jul 8, 2019

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants