how to limit the result grouped by the relationship Entity in Doctrine











up vote
0
down vote

favorite
1












Based on my example I try to get the last 5 comments from a Comment Entity, but only one comment for each Post Entity which is related to the Comment Entity.



Before I asked, I researched, but what I found here didn't make anything clear and didn't work either.



This my current query:



$lastComments = $this->em->getRepository('App:Comment')
->createQueryBuilder('c')
->select('p', 'c')
->leftJoin('c.post', 'p', 'WITH','p.id = c.post')
->andWhere('c.approved = :approved')->setParameter('approved', true)
->groupBy('p.id')
->orderBy("c.createdAt","desc")
->setMaxResults(5)
->getQuery()
->getResult();


Grouping the Post entity directly with groupBy throws me this error:



Syntax error or access violation: 1055 Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'symadmin.c0_.id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by").









share|improve this question


























    up vote
    0
    down vote

    favorite
    1












    Based on my example I try to get the last 5 comments from a Comment Entity, but only one comment for each Post Entity which is related to the Comment Entity.



    Before I asked, I researched, but what I found here didn't make anything clear and didn't work either.



    This my current query:



    $lastComments = $this->em->getRepository('App:Comment')
    ->createQueryBuilder('c')
    ->select('p', 'c')
    ->leftJoin('c.post', 'p', 'WITH','p.id = c.post')
    ->andWhere('c.approved = :approved')->setParameter('approved', true)
    ->groupBy('p.id')
    ->orderBy("c.createdAt","desc")
    ->setMaxResults(5)
    ->getQuery()
    ->getResult();


    Grouping the Post entity directly with groupBy throws me this error:



    Syntax error or access violation: 1055 Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'symadmin.c0_.id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by").









    share|improve this question
























      up vote
      0
      down vote

      favorite
      1









      up vote
      0
      down vote

      favorite
      1






      1





      Based on my example I try to get the last 5 comments from a Comment Entity, but only one comment for each Post Entity which is related to the Comment Entity.



      Before I asked, I researched, but what I found here didn't make anything clear and didn't work either.



      This my current query:



      $lastComments = $this->em->getRepository('App:Comment')
      ->createQueryBuilder('c')
      ->select('p', 'c')
      ->leftJoin('c.post', 'p', 'WITH','p.id = c.post')
      ->andWhere('c.approved = :approved')->setParameter('approved', true)
      ->groupBy('p.id')
      ->orderBy("c.createdAt","desc")
      ->setMaxResults(5)
      ->getQuery()
      ->getResult();


      Grouping the Post entity directly with groupBy throws me this error:



      Syntax error or access violation: 1055 Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'symadmin.c0_.id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by").









      share|improve this question













      Based on my example I try to get the last 5 comments from a Comment Entity, but only one comment for each Post Entity which is related to the Comment Entity.



      Before I asked, I researched, but what I found here didn't make anything clear and didn't work either.



      This my current query:



      $lastComments = $this->em->getRepository('App:Comment')
      ->createQueryBuilder('c')
      ->select('p', 'c')
      ->leftJoin('c.post', 'p', 'WITH','p.id = c.post')
      ->andWhere('c.approved = :approved')->setParameter('approved', true)
      ->groupBy('p.id')
      ->orderBy("c.createdAt","desc")
      ->setMaxResults(5)
      ->getQuery()
      ->getResult();


      Grouping the Post entity directly with groupBy throws me this error:



      Syntax error or access violation: 1055 Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'symadmin.c0_.id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by").






      mysql doctrine






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked yesterday









      jcarlosweb

      191419




      191419





























          active

          oldest

          votes











          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',
          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%2f53349263%2fhow-to-limit-the-result-grouped-by-the-relationship-entity-in-doctrine%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown






























          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53349263%2fhow-to-limit-the-result-grouped-by-the-relationship-entity-in-doctrine%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

          Create new schema in PostgreSQL using DBeaver

          Deepest pit of an array with Javascript: test on Codility

          Costa Masnaga