count of subquery in doctrine - querybuilder












2















I have simple query:



$this->qb->select('l.value')
->addSelect('count(l) AS cnt')
->addSelect('hour(l.time) AS date_hour')
->from(Logs::class, 'l')
->where('l.header = :header')
->groupBy('l.value')
->addGroupBy('date_hour')
->setParameter('header', 'someheader')


This code select 3 columns have one condition and two groupBy.



I want get records count of this query. Of course I dont want to download all records and check size of downloaded data.



Question:
How to rebuild this query and get result from db as singleScalarValue()?










share|improve this question

























  • This query does not return single scalar result. I have to rebuild it but I dont know how

    – Thomas Banderas
    Nov 16 '18 at 11:25











  • What u wanna count? Records, time or some value?

    – Odin Thunder
    Nov 16 '18 at 12:37
















2















I have simple query:



$this->qb->select('l.value')
->addSelect('count(l) AS cnt')
->addSelect('hour(l.time) AS date_hour')
->from(Logs::class, 'l')
->where('l.header = :header')
->groupBy('l.value')
->addGroupBy('date_hour')
->setParameter('header', 'someheader')


This code select 3 columns have one condition and two groupBy.



I want get records count of this query. Of course I dont want to download all records and check size of downloaded data.



Question:
How to rebuild this query and get result from db as singleScalarValue()?










share|improve this question

























  • This query does not return single scalar result. I have to rebuild it but I dont know how

    – Thomas Banderas
    Nov 16 '18 at 11:25











  • What u wanna count? Records, time or some value?

    – Odin Thunder
    Nov 16 '18 at 12:37














2












2








2








I have simple query:



$this->qb->select('l.value')
->addSelect('count(l) AS cnt')
->addSelect('hour(l.time) AS date_hour')
->from(Logs::class, 'l')
->where('l.header = :header')
->groupBy('l.value')
->addGroupBy('date_hour')
->setParameter('header', 'someheader')


This code select 3 columns have one condition and two groupBy.



I want get records count of this query. Of course I dont want to download all records and check size of downloaded data.



Question:
How to rebuild this query and get result from db as singleScalarValue()?










share|improve this question
















I have simple query:



$this->qb->select('l.value')
->addSelect('count(l) AS cnt')
->addSelect('hour(l.time) AS date_hour')
->from(Logs::class, 'l')
->where('l.header = :header')
->groupBy('l.value')
->addGroupBy('date_hour')
->setParameter('header', 'someheader')


This code select 3 columns have one condition and two groupBy.



I want get records count of this query. Of course I dont want to download all records and check size of downloaded data.



Question:
How to rebuild this query and get result from db as singleScalarValue()?







php symfony doctrine






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 11:26







Thomas Banderas

















asked Nov 16 '18 at 11:20









Thomas BanderasThomas Banderas

475222




475222













  • This query does not return single scalar result. I have to rebuild it but I dont know how

    – Thomas Banderas
    Nov 16 '18 at 11:25











  • What u wanna count? Records, time or some value?

    – Odin Thunder
    Nov 16 '18 at 12:37



















  • This query does not return single scalar result. I have to rebuild it but I dont know how

    – Thomas Banderas
    Nov 16 '18 at 11:25











  • What u wanna count? Records, time or some value?

    – Odin Thunder
    Nov 16 '18 at 12:37

















This query does not return single scalar result. I have to rebuild it but I dont know how

– Thomas Banderas
Nov 16 '18 at 11:25





This query does not return single scalar result. I have to rebuild it but I dont know how

– Thomas Banderas
Nov 16 '18 at 11:25













What u wanna count? Records, time or some value?

– Odin Thunder
Nov 16 '18 at 12:37





What u wanna count? Records, time or some value?

– Odin Thunder
Nov 16 '18 at 12:37












2 Answers
2






active

oldest

votes


















1














I think you should use (id column for count) here:



    ->addSelect('count(l) AS cnt')


Something like this, but if you show your Entity i can suggest right solution:



         $this->qb->select('l')
->addSelect('count(l.id) AS cnt')
->addSelect('hour(l.time) AS date_hour')
->from(Logs::class, 'l')
->where('l.header = :header')
->groupBy('l.value')
->addGroupBy('date_hour')
->setParameter('header', 'someheader')

$count = $qb->getQuery()->getSingleScalarResult();





share|improve this answer
























  • How from selected 3 columns get one single scalar result? :P

    – Thomas Banderas
    Nov 16 '18 at 11:30











  • Hmm.. i see.. probably custom query is the only way.

    – MorganFreeFarm
    Nov 16 '18 at 11:34



















0














Since GROUP BY + COUNT + getSingleScalarResult does not seem to work together in DQL queries, I guess your best option is to count afterwards



$sub
->select('l.id')
->addSelect('l.value as hidden val')
->addSelect('hour(l.time) AS hidden date_hour')
->from(Logs::class, 'l')
->where('l.header = :header')
->groupBy('val')
->addGroupBy('date_hour')
->setParameter('header', 'someheader')
;

$subIds = array_column($sub->getQuery()->getResult(), 'id');
$count = count($subIds);





share|improve this answer


























    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%2f53336843%2fcount-of-subquery-in-doctrine-querybuilder%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    I think you should use (id column for count) here:



        ->addSelect('count(l) AS cnt')


    Something like this, but if you show your Entity i can suggest right solution:



             $this->qb->select('l')
    ->addSelect('count(l.id) AS cnt')
    ->addSelect('hour(l.time) AS date_hour')
    ->from(Logs::class, 'l')
    ->where('l.header = :header')
    ->groupBy('l.value')
    ->addGroupBy('date_hour')
    ->setParameter('header', 'someheader')

    $count = $qb->getQuery()->getSingleScalarResult();





    share|improve this answer
























    • How from selected 3 columns get one single scalar result? :P

      – Thomas Banderas
      Nov 16 '18 at 11:30











    • Hmm.. i see.. probably custom query is the only way.

      – MorganFreeFarm
      Nov 16 '18 at 11:34
















    1














    I think you should use (id column for count) here:



        ->addSelect('count(l) AS cnt')


    Something like this, but if you show your Entity i can suggest right solution:



             $this->qb->select('l')
    ->addSelect('count(l.id) AS cnt')
    ->addSelect('hour(l.time) AS date_hour')
    ->from(Logs::class, 'l')
    ->where('l.header = :header')
    ->groupBy('l.value')
    ->addGroupBy('date_hour')
    ->setParameter('header', 'someheader')

    $count = $qb->getQuery()->getSingleScalarResult();





    share|improve this answer
























    • How from selected 3 columns get one single scalar result? :P

      – Thomas Banderas
      Nov 16 '18 at 11:30











    • Hmm.. i see.. probably custom query is the only way.

      – MorganFreeFarm
      Nov 16 '18 at 11:34














    1












    1








    1







    I think you should use (id column for count) here:



        ->addSelect('count(l) AS cnt')


    Something like this, but if you show your Entity i can suggest right solution:



             $this->qb->select('l')
    ->addSelect('count(l.id) AS cnt')
    ->addSelect('hour(l.time) AS date_hour')
    ->from(Logs::class, 'l')
    ->where('l.header = :header')
    ->groupBy('l.value')
    ->addGroupBy('date_hour')
    ->setParameter('header', 'someheader')

    $count = $qb->getQuery()->getSingleScalarResult();





    share|improve this answer













    I think you should use (id column for count) here:



        ->addSelect('count(l) AS cnt')


    Something like this, but if you show your Entity i can suggest right solution:



             $this->qb->select('l')
    ->addSelect('count(l.id) AS cnt')
    ->addSelect('hour(l.time) AS date_hour')
    ->from(Logs::class, 'l')
    ->where('l.header = :header')
    ->groupBy('l.value')
    ->addGroupBy('date_hour')
    ->setParameter('header', 'someheader')

    $count = $qb->getQuery()->getSingleScalarResult();






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 16 '18 at 11:29









    MorganFreeFarmMorganFreeFarm

    1,48431328




    1,48431328













    • How from selected 3 columns get one single scalar result? :P

      – Thomas Banderas
      Nov 16 '18 at 11:30











    • Hmm.. i see.. probably custom query is the only way.

      – MorganFreeFarm
      Nov 16 '18 at 11:34



















    • How from selected 3 columns get one single scalar result? :P

      – Thomas Banderas
      Nov 16 '18 at 11:30











    • Hmm.. i see.. probably custom query is the only way.

      – MorganFreeFarm
      Nov 16 '18 at 11:34

















    How from selected 3 columns get one single scalar result? :P

    – Thomas Banderas
    Nov 16 '18 at 11:30





    How from selected 3 columns get one single scalar result? :P

    – Thomas Banderas
    Nov 16 '18 at 11:30













    Hmm.. i see.. probably custom query is the only way.

    – MorganFreeFarm
    Nov 16 '18 at 11:34





    Hmm.. i see.. probably custom query is the only way.

    – MorganFreeFarm
    Nov 16 '18 at 11:34













    0














    Since GROUP BY + COUNT + getSingleScalarResult does not seem to work together in DQL queries, I guess your best option is to count afterwards



    $sub
    ->select('l.id')
    ->addSelect('l.value as hidden val')
    ->addSelect('hour(l.time) AS hidden date_hour')
    ->from(Logs::class, 'l')
    ->where('l.header = :header')
    ->groupBy('val')
    ->addGroupBy('date_hour')
    ->setParameter('header', 'someheader')
    ;

    $subIds = array_column($sub->getQuery()->getResult(), 'id');
    $count = count($subIds);





    share|improve this answer






























      0














      Since GROUP BY + COUNT + getSingleScalarResult does not seem to work together in DQL queries, I guess your best option is to count afterwards



      $sub
      ->select('l.id')
      ->addSelect('l.value as hidden val')
      ->addSelect('hour(l.time) AS hidden date_hour')
      ->from(Logs::class, 'l')
      ->where('l.header = :header')
      ->groupBy('val')
      ->addGroupBy('date_hour')
      ->setParameter('header', 'someheader')
      ;

      $subIds = array_column($sub->getQuery()->getResult(), 'id');
      $count = count($subIds);





      share|improve this answer




























        0












        0








        0







        Since GROUP BY + COUNT + getSingleScalarResult does not seem to work together in DQL queries, I guess your best option is to count afterwards



        $sub
        ->select('l.id')
        ->addSelect('l.value as hidden val')
        ->addSelect('hour(l.time) AS hidden date_hour')
        ->from(Logs::class, 'l')
        ->where('l.header = :header')
        ->groupBy('val')
        ->addGroupBy('date_hour')
        ->setParameter('header', 'someheader')
        ;

        $subIds = array_column($sub->getQuery()->getResult(), 'id');
        $count = count($subIds);





        share|improve this answer















        Since GROUP BY + COUNT + getSingleScalarResult does not seem to work together in DQL queries, I guess your best option is to count afterwards



        $sub
        ->select('l.id')
        ->addSelect('l.value as hidden val')
        ->addSelect('hour(l.time) AS hidden date_hour')
        ->from(Logs::class, 'l')
        ->where('l.header = :header')
        ->groupBy('val')
        ->addGroupBy('date_hour')
        ->setParameter('header', 'someheader')
        ;

        $subIds = array_column($sub->getQuery()->getResult(), 'id');
        $count = count($subIds);






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 16 '18 at 22:42

























        answered Nov 16 '18 at 20:39









        JulesezaarJulesezaar

        42828




        42828






























            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%2f53336843%2fcount-of-subquery-in-doctrine-querybuilder%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

            Xamarin.iOS Cant Deploy on Iphone

            Glorious Revolution

            Dulmage-Mendelsohn matrix decomposition in Python