Happyr/Doctrine-Specification

Auto-joins with dot notation

alex-barylski opened this issue · 7 comments

https://github.com/Happyr/Doctrine-Specification/blob/2.x/UPGRADE.md

Suggest that something like the following would be possible:

        $specification = Spec::andX(
            Spec::eq('complex.code', 'WC11')
        );

The root entity has a relation to a child named complex, #which has a field named code

However the above doesn't work and results in something like:

SELECT root FROM MyRoot root WHERE root.complex.code = :comparison_0 ORDER BY root.id DESC

Is that dotted notation a feature of Doctrine or is that resolved by this library? I'm running Doctrine 2.10.4

Thoughts?

Dot notation is resolved by this library. It does not rely on Doctrine mapping.

Have you changed the behavior of the DQLContextResolver? Check before and after executing the specification what settings you have specified.

DQLContextResolver::isDeadJoinsProtectionEnabled();
DQLContextResolver::isConflictProtectionEnabled();
DQLContextResolver::isAutoJoiningEnabled();
DQLContextResolver::isUniqueAliasAlwaysUsed();

Thank you for the speedy response.

I haven't changed anything at all at this point, just installed the package and in my Symfony controller::action have a simple bit of code:

        //$specification = Spec::andX(
        //    Spec::innerJoin('complex', 'c'),
        //    Spec::eq('complex', 1)
        //);

       // Does not work?!?
        //$specification = Spec::eq('complex.code', 1);

        $specification = Spec::eq('complex', 1);

Maybe worth noting, I did actually create a Specification class (ie: DemoSpec) to see if maybe only that name resolution / auto-joins happened in that context but got the same result.

The examples you provided are not equivalent.

This code

$specification = Spec::eq('complex.code', 1);

is equivalent to

$specification = Spec::andX(
    Spec::innerJoin('complex', 'c'),
    Spec::eq('code', 1, 'c')
);

Sorry, I'm still confused as to what I'm doing wrong, using this code:

        $specification = Spec::andX(
            Spec::innerJoin('complex', 'c'),
            Spec::eq('code', 1, 'c')
        );

Results in this:

QueryException
SELECT root FROM MyRoot root INNER JOIN root.complex c INNER JOIN root.c c61defe75d51b3 WHERE c61defe75d51b3.code = :comparison_0 ORDER BY root.id DESC

or this code:

$specification = Spec::eq('complex.code', 'WC11');

Results in this:

QueryException
SELECT root FROM MyRoot root WHERE root.complex.code = :comparison_0 ORDER BY root.id DESC

What am I doing wrong, what am I misunderstanding of the use of the API?

p.s-I just noticed the DQL being generated has two INNER JOIN's?

Thank you kindly,
Alex

I'm sorry. Error in the documentation. It would be correct to do this:

$specification = Spec::eq('code', 'WC11', 'complex');

@alex-barylski does this solve your problem?

Yes it did, thank you.