Doctrine 2 DQL - how to select inverse side of unidirectional many-to-many query?












18















I have two classes - Page and SiteVersion, which have a many to many relationship. Only SiteVersion is aware of the relationship (because the site is modular and I want to be able to take away and drop in the module that SiteVersion belongs to).



How would I therefore select pages based on criteria of SiteVersion?



For example, this doesn't work:



SELECT p FROM SiteVersion v JOIN v.pages p WHERE v.id = 5 AND p.slug='index'


I get the error:



[DoctrineORMQueryQueryException]
[Semantical Error] line 0, col -1 near 'SELECT p FROM': Error: Cannot select entity through identification variables without choosing at least one root entity alias.


Even though I can select "v" with this query.



I think I could possibly resolve this by introducing a class for the relationship (a PageToVersion class) but is there any way without doing that, or making it bidirectional?










share|improve this question























  • Hey Gnuffo1, Could you please accept @Ocramius's answer, I think there is no doubt this solved your issue.

    – Mick
    Jan 7 '14 at 7:01
















18















I have two classes - Page and SiteVersion, which have a many to many relationship. Only SiteVersion is aware of the relationship (because the site is modular and I want to be able to take away and drop in the module that SiteVersion belongs to).



How would I therefore select pages based on criteria of SiteVersion?



For example, this doesn't work:



SELECT p FROM SiteVersion v JOIN v.pages p WHERE v.id = 5 AND p.slug='index'


I get the error:



[DoctrineORMQueryQueryException]
[Semantical Error] line 0, col -1 near 'SELECT p FROM': Error: Cannot select entity through identification variables without choosing at least one root entity alias.


Even though I can select "v" with this query.



I think I could possibly resolve this by introducing a class for the relationship (a PageToVersion class) but is there any way without doing that, or making it bidirectional?










share|improve this question























  • Hey Gnuffo1, Could you please accept @Ocramius's answer, I think there is no doubt this solved your issue.

    – Mick
    Jan 7 '14 at 7:01














18












18








18


9






I have two classes - Page and SiteVersion, which have a many to many relationship. Only SiteVersion is aware of the relationship (because the site is modular and I want to be able to take away and drop in the module that SiteVersion belongs to).



How would I therefore select pages based on criteria of SiteVersion?



For example, this doesn't work:



SELECT p FROM SiteVersion v JOIN v.pages p WHERE v.id = 5 AND p.slug='index'


I get the error:



[DoctrineORMQueryQueryException]
[Semantical Error] line 0, col -1 near 'SELECT p FROM': Error: Cannot select entity through identification variables without choosing at least one root entity alias.


Even though I can select "v" with this query.



I think I could possibly resolve this by introducing a class for the relationship (a PageToVersion class) but is there any way without doing that, or making it bidirectional?










share|improve this question














I have two classes - Page and SiteVersion, which have a many to many relationship. Only SiteVersion is aware of the relationship (because the site is modular and I want to be able to take away and drop in the module that SiteVersion belongs to).



How would I therefore select pages based on criteria of SiteVersion?



For example, this doesn't work:



SELECT p FROM SiteVersion v JOIN v.pages p WHERE v.id = 5 AND p.slug='index'


I get the error:



[DoctrineORMQueryQueryException]
[Semantical Error] line 0, col -1 near 'SELECT p FROM': Error: Cannot select entity through identification variables without choosing at least one root entity alias.


Even though I can select "v" with this query.



I think I could possibly resolve this by introducing a class for the relationship (a PageToVersion class) but is there any way without doing that, or making it bidirectional?







php doctrine dql






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 25 '11 at 12:28









Gnuffo1Gnuffo1

861102747




861102747













  • Hey Gnuffo1, Could you please accept @Ocramius's answer, I think there is no doubt this solved your issue.

    – Mick
    Jan 7 '14 at 7:01



















  • Hey Gnuffo1, Could you please accept @Ocramius's answer, I think there is no doubt this solved your issue.

    – Mick
    Jan 7 '14 at 7:01

















Hey Gnuffo1, Could you please accept @Ocramius's answer, I think there is no doubt this solved your issue.

– Mick
Jan 7 '14 at 7:01





Hey Gnuffo1, Could you please accept @Ocramius's answer, I think there is no doubt this solved your issue.

– Mick
Jan 7 '14 at 7:01












5 Answers
5






active

oldest

votes


















24














There's two ways of handling this in Doctrine ORM. The most typical one is using an IN condition with a subquery:



SELECT
p
FROM
SitePage p
WHERE
p.id IN(
SELECT
p2.id
FROM
SiteVersion v
JOIN
v.pages p2
WHERE
v.id = :versionId
AND
p.slug = :slug
)


The other way is with an additional join with the arbitrary join functionality introduced in version 2.3 of the ORM:



SELECT
p
FROM
SitePage p
JOIN
SiteVersion v
WITH
1 = 1
JOIN
v.pages p2
WHERE
p.id = p2.id
AND
v.id = :versionId
AND
p2.slug = :slug


The 1 = 1 is just because of a current limitation of the parser.



Please note that the limitation that causes the semantical error is because the hydration process starts from the root of the selected entities. Without a root in place, the hydrator has no reference on how to collapse fetch-joined or joined results.






share|improve this answer


























  • Is this still the best practice for this scenario or is there something for this now in doctrine 2.5?

    – Steffen Brem
    Mar 15 '16 at 23:38













  • Yes, this didn't change.

    – Ocramius
    Mar 16 '16 at 12:13











  • Also I would like to know if this impacts performance (the 1=1 part)? Otherwise I would have to look into another solution for this, I have a lot of DQL queries that need to join like this.

    – Steffen Brem
    Nov 3 '16 at 13:05






  • 1





    No, decent SQL planners strip the comparison away before executing the query.

    – Ocramius
    Nov 7 '16 at 9:48



















2














I think you need to select the SiteVersion in your query too:



SELECT v, p FROM SiteVersion v JOIN v.pages p WHERE v.id = 5 AND p.slug='index'


You will get an array of SiteVersion entities which you can loop through to get the Page entities.






share|improve this answer



















  • 1





    This will fetch-join the site pages into the site versions, returning a list of SiteVersion objects, and not just the site pages. This is actually wrong given the expected resultset (as of the question).

    – Ocramius
    Mar 16 '13 at 1:56



















1














I couldn't figure out how to get native queries working, so have resolved in a slightly hacky way:



$id = $em->getConnection()->fetchColumn("SELECT
pages.id
FROM
pages
INNER JOIN siteversion_page ON siteversion_page.page_id = pages.id
INNER JOIN siteversions ON siteversion_page.siteversion_id = siteversions.id
WHERE siteversions.id = 1
AND pages.slug = 'index'");

$page = $em->find('Page', $id);


I don't like it because it results in more queries to the database (especially if I need to fetch an array of pages instead of one) but it works.



Edit: I've decided to just go with a class for the association. Now I can do this query:



SELECT p FROM Page p, SiteVersionPageLink l
WHERE l.page = p AND l.siteVersion = 5 AND p.slug = 'index'





share|improve this answer


























  • You don't really need the association if this kind of traversing is rare. Yes, it simplifies things, but you get some performance drawbacks when hydrating your objects.

    – Ocramius
    Mar 16 '13 at 2:02



















1














Try this (or something like it):



SELECT p FROM Page p WHERE EXISTS (SELECT v FROM SiteVersion v WHERE p MEMBER OF v.pages AND v.id = 5 AND p.slug = 'index')


I haven't tested this exactly, but I have gotten something similar to work. The use of EXISTS and MEMBER OF are buried in the DQL Select Examples section of the DQL chapter.






share|improve this answer

































    0














    I've found a possible solution for this problem here.



    According to that page, your query should look something like this:



    SELECT p FROM SiteVersion v, Page p WHERE v.id = 5 AND p.slug='index' AND v.page = p;



    Does it solve your problem?






    share|improve this answer
























    • No. The problem is, it's a many-to-many, not one-to-many. It's v.pages, not v.page but even if I do: "SELECT p FROM SiteVersion v, Page p WHERE v.id = 5 AND p.slug='index' AND v.pages = p" that doesn't work either (Error: Invalid PathExpression. StateFieldPathExpression or SingleValuedAssociationField expected.)

      – Gnuffo1
      Mar 25 '11 at 13:04













    • Then why not you specify the other side of the relationship in your schema definition?

      – Imi Borbas
      Mar 25 '11 at 13:07











    • Because I want to be able to just drop this module in without affecting anything that's already there.

      – Gnuffo1
      Mar 25 '11 at 13:10











    • Is using native sql to select at least the page id-s is a viable option for you? Of course it would be much cleaner to use DQL...

      – Imi Borbas
      Mar 25 '11 at 13:13











    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f5432404%2fdoctrine-2-dql-how-to-select-inverse-side-of-unidirectional-many-to-many-query%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    5 Answers
    5






    active

    oldest

    votes








    5 Answers
    5






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    24














    There's two ways of handling this in Doctrine ORM. The most typical one is using an IN condition with a subquery:



    SELECT
    p
    FROM
    SitePage p
    WHERE
    p.id IN(
    SELECT
    p2.id
    FROM
    SiteVersion v
    JOIN
    v.pages p2
    WHERE
    v.id = :versionId
    AND
    p.slug = :slug
    )


    The other way is with an additional join with the arbitrary join functionality introduced in version 2.3 of the ORM:



    SELECT
    p
    FROM
    SitePage p
    JOIN
    SiteVersion v
    WITH
    1 = 1
    JOIN
    v.pages p2
    WHERE
    p.id = p2.id
    AND
    v.id = :versionId
    AND
    p2.slug = :slug


    The 1 = 1 is just because of a current limitation of the parser.



    Please note that the limitation that causes the semantical error is because the hydration process starts from the root of the selected entities. Without a root in place, the hydrator has no reference on how to collapse fetch-joined or joined results.






    share|improve this answer


























    • Is this still the best practice for this scenario or is there something for this now in doctrine 2.5?

      – Steffen Brem
      Mar 15 '16 at 23:38













    • Yes, this didn't change.

      – Ocramius
      Mar 16 '16 at 12:13











    • Also I would like to know if this impacts performance (the 1=1 part)? Otherwise I would have to look into another solution for this, I have a lot of DQL queries that need to join like this.

      – Steffen Brem
      Nov 3 '16 at 13:05






    • 1





      No, decent SQL planners strip the comparison away before executing the query.

      – Ocramius
      Nov 7 '16 at 9:48
















    24














    There's two ways of handling this in Doctrine ORM. The most typical one is using an IN condition with a subquery:



    SELECT
    p
    FROM
    SitePage p
    WHERE
    p.id IN(
    SELECT
    p2.id
    FROM
    SiteVersion v
    JOIN
    v.pages p2
    WHERE
    v.id = :versionId
    AND
    p.slug = :slug
    )


    The other way is with an additional join with the arbitrary join functionality introduced in version 2.3 of the ORM:



    SELECT
    p
    FROM
    SitePage p
    JOIN
    SiteVersion v
    WITH
    1 = 1
    JOIN
    v.pages p2
    WHERE
    p.id = p2.id
    AND
    v.id = :versionId
    AND
    p2.slug = :slug


    The 1 = 1 is just because of a current limitation of the parser.



    Please note that the limitation that causes the semantical error is because the hydration process starts from the root of the selected entities. Without a root in place, the hydrator has no reference on how to collapse fetch-joined or joined results.






    share|improve this answer


























    • Is this still the best practice for this scenario or is there something for this now in doctrine 2.5?

      – Steffen Brem
      Mar 15 '16 at 23:38













    • Yes, this didn't change.

      – Ocramius
      Mar 16 '16 at 12:13











    • Also I would like to know if this impacts performance (the 1=1 part)? Otherwise I would have to look into another solution for this, I have a lot of DQL queries that need to join like this.

      – Steffen Brem
      Nov 3 '16 at 13:05






    • 1





      No, decent SQL planners strip the comparison away before executing the query.

      – Ocramius
      Nov 7 '16 at 9:48














    24












    24








    24







    There's two ways of handling this in Doctrine ORM. The most typical one is using an IN condition with a subquery:



    SELECT
    p
    FROM
    SitePage p
    WHERE
    p.id IN(
    SELECT
    p2.id
    FROM
    SiteVersion v
    JOIN
    v.pages p2
    WHERE
    v.id = :versionId
    AND
    p.slug = :slug
    )


    The other way is with an additional join with the arbitrary join functionality introduced in version 2.3 of the ORM:



    SELECT
    p
    FROM
    SitePage p
    JOIN
    SiteVersion v
    WITH
    1 = 1
    JOIN
    v.pages p2
    WHERE
    p.id = p2.id
    AND
    v.id = :versionId
    AND
    p2.slug = :slug


    The 1 = 1 is just because of a current limitation of the parser.



    Please note that the limitation that causes the semantical error is because the hydration process starts from the root of the selected entities. Without a root in place, the hydrator has no reference on how to collapse fetch-joined or joined results.






    share|improve this answer















    There's two ways of handling this in Doctrine ORM. The most typical one is using an IN condition with a subquery:



    SELECT
    p
    FROM
    SitePage p
    WHERE
    p.id IN(
    SELECT
    p2.id
    FROM
    SiteVersion v
    JOIN
    v.pages p2
    WHERE
    v.id = :versionId
    AND
    p.slug = :slug
    )


    The other way is with an additional join with the arbitrary join functionality introduced in version 2.3 of the ORM:



    SELECT
    p
    FROM
    SitePage p
    JOIN
    SiteVersion v
    WITH
    1 = 1
    JOIN
    v.pages p2
    WHERE
    p.id = p2.id
    AND
    v.id = :versionId
    AND
    p2.slug = :slug


    The 1 = 1 is just because of a current limitation of the parser.



    Please note that the limitation that causes the semantical error is because the hydration process starts from the root of the selected entities. Without a root in place, the hydrator has no reference on how to collapse fetch-joined or joined results.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 13 '18 at 15:58









    Thomas Landauer

    2,30811647




    2,30811647










    answered Mar 16 '13 at 1:53









    OcramiusOcramius

    21.3k58191




    21.3k58191













    • Is this still the best practice for this scenario or is there something for this now in doctrine 2.5?

      – Steffen Brem
      Mar 15 '16 at 23:38













    • Yes, this didn't change.

      – Ocramius
      Mar 16 '16 at 12:13











    • Also I would like to know if this impacts performance (the 1=1 part)? Otherwise I would have to look into another solution for this, I have a lot of DQL queries that need to join like this.

      – Steffen Brem
      Nov 3 '16 at 13:05






    • 1





      No, decent SQL planners strip the comparison away before executing the query.

      – Ocramius
      Nov 7 '16 at 9:48



















    • Is this still the best practice for this scenario or is there something for this now in doctrine 2.5?

      – Steffen Brem
      Mar 15 '16 at 23:38













    • Yes, this didn't change.

      – Ocramius
      Mar 16 '16 at 12:13











    • Also I would like to know if this impacts performance (the 1=1 part)? Otherwise I would have to look into another solution for this, I have a lot of DQL queries that need to join like this.

      – Steffen Brem
      Nov 3 '16 at 13:05






    • 1





      No, decent SQL planners strip the comparison away before executing the query.

      – Ocramius
      Nov 7 '16 at 9:48

















    Is this still the best practice for this scenario or is there something for this now in doctrine 2.5?

    – Steffen Brem
    Mar 15 '16 at 23:38







    Is this still the best practice for this scenario or is there something for this now in doctrine 2.5?

    – Steffen Brem
    Mar 15 '16 at 23:38















    Yes, this didn't change.

    – Ocramius
    Mar 16 '16 at 12:13





    Yes, this didn't change.

    – Ocramius
    Mar 16 '16 at 12:13













    Also I would like to know if this impacts performance (the 1=1 part)? Otherwise I would have to look into another solution for this, I have a lot of DQL queries that need to join like this.

    – Steffen Brem
    Nov 3 '16 at 13:05





    Also I would like to know if this impacts performance (the 1=1 part)? Otherwise I would have to look into another solution for this, I have a lot of DQL queries that need to join like this.

    – Steffen Brem
    Nov 3 '16 at 13:05




    1




    1





    No, decent SQL planners strip the comparison away before executing the query.

    – Ocramius
    Nov 7 '16 at 9:48





    No, decent SQL planners strip the comparison away before executing the query.

    – Ocramius
    Nov 7 '16 at 9:48













    2














    I think you need to select the SiteVersion in your query too:



    SELECT v, p FROM SiteVersion v JOIN v.pages p WHERE v.id = 5 AND p.slug='index'


    You will get an array of SiteVersion entities which you can loop through to get the Page entities.






    share|improve this answer



















    • 1





      This will fetch-join the site pages into the site versions, returning a list of SiteVersion objects, and not just the site pages. This is actually wrong given the expected resultset (as of the question).

      – Ocramius
      Mar 16 '13 at 1:56
















    2














    I think you need to select the SiteVersion in your query too:



    SELECT v, p FROM SiteVersion v JOIN v.pages p WHERE v.id = 5 AND p.slug='index'


    You will get an array of SiteVersion entities which you can loop through to get the Page entities.






    share|improve this answer



















    • 1





      This will fetch-join the site pages into the site versions, returning a list of SiteVersion objects, and not just the site pages. This is actually wrong given the expected resultset (as of the question).

      – Ocramius
      Mar 16 '13 at 1:56














    2












    2








    2







    I think you need to select the SiteVersion in your query too:



    SELECT v, p FROM SiteVersion v JOIN v.pages p WHERE v.id = 5 AND p.slug='index'


    You will get an array of SiteVersion entities which you can loop through to get the Page entities.






    share|improve this answer













    I think you need to select the SiteVersion in your query too:



    SELECT v, p FROM SiteVersion v JOIN v.pages p WHERE v.id = 5 AND p.slug='index'


    You will get an array of SiteVersion entities which you can loop through to get the Page entities.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Mar 25 '11 at 17:08









    rojocarojoca

    9,48343541




    9,48343541








    • 1





      This will fetch-join the site pages into the site versions, returning a list of SiteVersion objects, and not just the site pages. This is actually wrong given the expected resultset (as of the question).

      – Ocramius
      Mar 16 '13 at 1:56














    • 1





      This will fetch-join the site pages into the site versions, returning a list of SiteVersion objects, and not just the site pages. This is actually wrong given the expected resultset (as of the question).

      – Ocramius
      Mar 16 '13 at 1:56








    1




    1





    This will fetch-join the site pages into the site versions, returning a list of SiteVersion objects, and not just the site pages. This is actually wrong given the expected resultset (as of the question).

    – Ocramius
    Mar 16 '13 at 1:56





    This will fetch-join the site pages into the site versions, returning a list of SiteVersion objects, and not just the site pages. This is actually wrong given the expected resultset (as of the question).

    – Ocramius
    Mar 16 '13 at 1:56











    1














    I couldn't figure out how to get native queries working, so have resolved in a slightly hacky way:



    $id = $em->getConnection()->fetchColumn("SELECT
    pages.id
    FROM
    pages
    INNER JOIN siteversion_page ON siteversion_page.page_id = pages.id
    INNER JOIN siteversions ON siteversion_page.siteversion_id = siteversions.id
    WHERE siteversions.id = 1
    AND pages.slug = 'index'");

    $page = $em->find('Page', $id);


    I don't like it because it results in more queries to the database (especially if I need to fetch an array of pages instead of one) but it works.



    Edit: I've decided to just go with a class for the association. Now I can do this query:



    SELECT p FROM Page p, SiteVersionPageLink l
    WHERE l.page = p AND l.siteVersion = 5 AND p.slug = 'index'





    share|improve this answer


























    • You don't really need the association if this kind of traversing is rare. Yes, it simplifies things, but you get some performance drawbacks when hydrating your objects.

      – Ocramius
      Mar 16 '13 at 2:02
















    1














    I couldn't figure out how to get native queries working, so have resolved in a slightly hacky way:



    $id = $em->getConnection()->fetchColumn("SELECT
    pages.id
    FROM
    pages
    INNER JOIN siteversion_page ON siteversion_page.page_id = pages.id
    INNER JOIN siteversions ON siteversion_page.siteversion_id = siteversions.id
    WHERE siteversions.id = 1
    AND pages.slug = 'index'");

    $page = $em->find('Page', $id);


    I don't like it because it results in more queries to the database (especially if I need to fetch an array of pages instead of one) but it works.



    Edit: I've decided to just go with a class for the association. Now I can do this query:



    SELECT p FROM Page p, SiteVersionPageLink l
    WHERE l.page = p AND l.siteVersion = 5 AND p.slug = 'index'





    share|improve this answer


























    • You don't really need the association if this kind of traversing is rare. Yes, it simplifies things, but you get some performance drawbacks when hydrating your objects.

      – Ocramius
      Mar 16 '13 at 2:02














    1












    1








    1







    I couldn't figure out how to get native queries working, so have resolved in a slightly hacky way:



    $id = $em->getConnection()->fetchColumn("SELECT
    pages.id
    FROM
    pages
    INNER JOIN siteversion_page ON siteversion_page.page_id = pages.id
    INNER JOIN siteversions ON siteversion_page.siteversion_id = siteversions.id
    WHERE siteversions.id = 1
    AND pages.slug = 'index'");

    $page = $em->find('Page', $id);


    I don't like it because it results in more queries to the database (especially if I need to fetch an array of pages instead of one) but it works.



    Edit: I've decided to just go with a class for the association. Now I can do this query:



    SELECT p FROM Page p, SiteVersionPageLink l
    WHERE l.page = p AND l.siteVersion = 5 AND p.slug = 'index'





    share|improve this answer















    I couldn't figure out how to get native queries working, so have resolved in a slightly hacky way:



    $id = $em->getConnection()->fetchColumn("SELECT
    pages.id
    FROM
    pages
    INNER JOIN siteversion_page ON siteversion_page.page_id = pages.id
    INNER JOIN siteversions ON siteversion_page.siteversion_id = siteversions.id
    WHERE siteversions.id = 1
    AND pages.slug = 'index'");

    $page = $em->find('Page', $id);


    I don't like it because it results in more queries to the database (especially if I need to fetch an array of pages instead of one) but it works.



    Edit: I've decided to just go with a class for the association. Now I can do this query:



    SELECT p FROM Page p, SiteVersionPageLink l
    WHERE l.page = p AND l.siteVersion = 5 AND p.slug = 'index'






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Mar 28 '11 at 12:47

























    answered Mar 25 '11 at 14:51









    Gnuffo1Gnuffo1

    861102747




    861102747













    • You don't really need the association if this kind of traversing is rare. Yes, it simplifies things, but you get some performance drawbacks when hydrating your objects.

      – Ocramius
      Mar 16 '13 at 2:02



















    • You don't really need the association if this kind of traversing is rare. Yes, it simplifies things, but you get some performance drawbacks when hydrating your objects.

      – Ocramius
      Mar 16 '13 at 2:02

















    You don't really need the association if this kind of traversing is rare. Yes, it simplifies things, but you get some performance drawbacks when hydrating your objects.

    – Ocramius
    Mar 16 '13 at 2:02





    You don't really need the association if this kind of traversing is rare. Yes, it simplifies things, but you get some performance drawbacks when hydrating your objects.

    – Ocramius
    Mar 16 '13 at 2:02











    1














    Try this (or something like it):



    SELECT p FROM Page p WHERE EXISTS (SELECT v FROM SiteVersion v WHERE p MEMBER OF v.pages AND v.id = 5 AND p.slug = 'index')


    I haven't tested this exactly, but I have gotten something similar to work. The use of EXISTS and MEMBER OF are buried in the DQL Select Examples section of the DQL chapter.






    share|improve this answer






























      1














      Try this (or something like it):



      SELECT p FROM Page p WHERE EXISTS (SELECT v FROM SiteVersion v WHERE p MEMBER OF v.pages AND v.id = 5 AND p.slug = 'index')


      I haven't tested this exactly, but I have gotten something similar to work. The use of EXISTS and MEMBER OF are buried in the DQL Select Examples section of the DQL chapter.






      share|improve this answer




























        1












        1








        1







        Try this (or something like it):



        SELECT p FROM Page p WHERE EXISTS (SELECT v FROM SiteVersion v WHERE p MEMBER OF v.pages AND v.id = 5 AND p.slug = 'index')


        I haven't tested this exactly, but I have gotten something similar to work. The use of EXISTS and MEMBER OF are buried in the DQL Select Examples section of the DQL chapter.






        share|improve this answer















        Try this (or something like it):



        SELECT p FROM Page p WHERE EXISTS (SELECT v FROM SiteVersion v WHERE p MEMBER OF v.pages AND v.id = 5 AND p.slug = 'index')


        I haven't tested this exactly, but I have gotten something similar to work. The use of EXISTS and MEMBER OF are buried in the DQL Select Examples section of the DQL chapter.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Mar 15 '13 at 6:50









        Jon L.

        1,25221328




        1,25221328










        answered Jul 11 '12 at 22:44









        Ian PhillipsIan Phillips

        1,41711429




        1,41711429























            0














            I've found a possible solution for this problem here.



            According to that page, your query should look something like this:



            SELECT p FROM SiteVersion v, Page p WHERE v.id = 5 AND p.slug='index' AND v.page = p;



            Does it solve your problem?






            share|improve this answer
























            • No. The problem is, it's a many-to-many, not one-to-many. It's v.pages, not v.page but even if I do: "SELECT p FROM SiteVersion v, Page p WHERE v.id = 5 AND p.slug='index' AND v.pages = p" that doesn't work either (Error: Invalid PathExpression. StateFieldPathExpression or SingleValuedAssociationField expected.)

              – Gnuffo1
              Mar 25 '11 at 13:04













            • Then why not you specify the other side of the relationship in your schema definition?

              – Imi Borbas
              Mar 25 '11 at 13:07











            • Because I want to be able to just drop this module in without affecting anything that's already there.

              – Gnuffo1
              Mar 25 '11 at 13:10











            • Is using native sql to select at least the page id-s is a viable option for you? Of course it would be much cleaner to use DQL...

              – Imi Borbas
              Mar 25 '11 at 13:13
















            0














            I've found a possible solution for this problem here.



            According to that page, your query should look something like this:



            SELECT p FROM SiteVersion v, Page p WHERE v.id = 5 AND p.slug='index' AND v.page = p;



            Does it solve your problem?






            share|improve this answer
























            • No. The problem is, it's a many-to-many, not one-to-many. It's v.pages, not v.page but even if I do: "SELECT p FROM SiteVersion v, Page p WHERE v.id = 5 AND p.slug='index' AND v.pages = p" that doesn't work either (Error: Invalid PathExpression. StateFieldPathExpression or SingleValuedAssociationField expected.)

              – Gnuffo1
              Mar 25 '11 at 13:04













            • Then why not you specify the other side of the relationship in your schema definition?

              – Imi Borbas
              Mar 25 '11 at 13:07











            • Because I want to be able to just drop this module in without affecting anything that's already there.

              – Gnuffo1
              Mar 25 '11 at 13:10











            • Is using native sql to select at least the page id-s is a viable option for you? Of course it would be much cleaner to use DQL...

              – Imi Borbas
              Mar 25 '11 at 13:13














            0












            0








            0







            I've found a possible solution for this problem here.



            According to that page, your query should look something like this:



            SELECT p FROM SiteVersion v, Page p WHERE v.id = 5 AND p.slug='index' AND v.page = p;



            Does it solve your problem?






            share|improve this answer













            I've found a possible solution for this problem here.



            According to that page, your query should look something like this:



            SELECT p FROM SiteVersion v, Page p WHERE v.id = 5 AND p.slug='index' AND v.page = p;



            Does it solve your problem?







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Mar 25 '11 at 12:42









            Imi BorbasImi Borbas

            2,94811316




            2,94811316













            • No. The problem is, it's a many-to-many, not one-to-many. It's v.pages, not v.page but even if I do: "SELECT p FROM SiteVersion v, Page p WHERE v.id = 5 AND p.slug='index' AND v.pages = p" that doesn't work either (Error: Invalid PathExpression. StateFieldPathExpression or SingleValuedAssociationField expected.)

              – Gnuffo1
              Mar 25 '11 at 13:04













            • Then why not you specify the other side of the relationship in your schema definition?

              – Imi Borbas
              Mar 25 '11 at 13:07











            • Because I want to be able to just drop this module in without affecting anything that's already there.

              – Gnuffo1
              Mar 25 '11 at 13:10











            • Is using native sql to select at least the page id-s is a viable option for you? Of course it would be much cleaner to use DQL...

              – Imi Borbas
              Mar 25 '11 at 13:13



















            • No. The problem is, it's a many-to-many, not one-to-many. It's v.pages, not v.page but even if I do: "SELECT p FROM SiteVersion v, Page p WHERE v.id = 5 AND p.slug='index' AND v.pages = p" that doesn't work either (Error: Invalid PathExpression. StateFieldPathExpression or SingleValuedAssociationField expected.)

              – Gnuffo1
              Mar 25 '11 at 13:04













            • Then why not you specify the other side of the relationship in your schema definition?

              – Imi Borbas
              Mar 25 '11 at 13:07











            • Because I want to be able to just drop this module in without affecting anything that's already there.

              – Gnuffo1
              Mar 25 '11 at 13:10











            • Is using native sql to select at least the page id-s is a viable option for you? Of course it would be much cleaner to use DQL...

              – Imi Borbas
              Mar 25 '11 at 13:13

















            No. The problem is, it's a many-to-many, not one-to-many. It's v.pages, not v.page but even if I do: "SELECT p FROM SiteVersion v, Page p WHERE v.id = 5 AND p.slug='index' AND v.pages = p" that doesn't work either (Error: Invalid PathExpression. StateFieldPathExpression or SingleValuedAssociationField expected.)

            – Gnuffo1
            Mar 25 '11 at 13:04







            No. The problem is, it's a many-to-many, not one-to-many. It's v.pages, not v.page but even if I do: "SELECT p FROM SiteVersion v, Page p WHERE v.id = 5 AND p.slug='index' AND v.pages = p" that doesn't work either (Error: Invalid PathExpression. StateFieldPathExpression or SingleValuedAssociationField expected.)

            – Gnuffo1
            Mar 25 '11 at 13:04















            Then why not you specify the other side of the relationship in your schema definition?

            – Imi Borbas
            Mar 25 '11 at 13:07





            Then why not you specify the other side of the relationship in your schema definition?

            – Imi Borbas
            Mar 25 '11 at 13:07













            Because I want to be able to just drop this module in without affecting anything that's already there.

            – Gnuffo1
            Mar 25 '11 at 13:10





            Because I want to be able to just drop this module in without affecting anything that's already there.

            – Gnuffo1
            Mar 25 '11 at 13:10













            Is using native sql to select at least the page id-s is a viable option for you? Of course it would be much cleaner to use DQL...

            – Imi Borbas
            Mar 25 '11 at 13:13





            Is using native sql to select at least the page id-s is a viable option for you? Of course it would be much cleaner to use DQL...

            – Imi Borbas
            Mar 25 '11 at 13:13


















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f5432404%2fdoctrine-2-dql-how-to-select-inverse-side-of-unidirectional-many-to-many-query%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Bressuire

            Vorschmack

            Quarantine