Result Data structure of Doctrine Join












0















Using this Query:



$qb = $this->createQueryBuilder('r');
$qb->leftJoin('r.users', 'u')
->addSelect('count(u.id) as user_count')
->groupBy('r.id');


I get result-set:



Array
(
[0] => Array
(
[0] => Array
(
[id] => 45
[role_name] => ROLE_ADMIN
[description] => Admin roles
)

[user_count] => 1
)

[1] => Array
(
[0] => Array
(
[id] => 47
[role_name] => ROLE_OPERATOR
[description] => Operator role
)

[user_count] => 1
)

[2] => Array
(
[0] => Array
(
[id] => 48
[role_name] => ROLE_TEST
[description] => ROLE_TEST
)

[user_count] => 0
)

)


Is there a smart way of transforming all the properties the same level? As now a roles are in sub Array with 0 key.



Desired result:



Array
(
[0] => Array
(
[id] => 45
[role_name] => ROLE_ADMIN
[description] => Admin roles
[user_count] => 1
)

[1] => Array
(
[id] => 47
[role_name] => ROLE_OPERATOR
[description] => Operator role
[user_count] => 1
)

[2] => Array
(
[id] => 48
[role_name] => ROLE_TEST
[description] => ROLE_TEST
[user_count] => 0
)

)









share|improve this question























  • I think there is no chance to this, because the first select is the object itselfs and the second is an additionally select stmt. If user_count would be an attribute of your object this would work.

    – Fabian
    Nov 20 '18 at 13:00











  • But I think you can count the users on your $role object if you have an ->getUsers() method in your Role class?

    – Fabian
    Nov 20 '18 at 13:02











  • @Fabian, yes, there is Role::getUser() method in a entity. How do I add count to a result-set in this case?

    – Roman Newaza
    Nov 20 '18 at 13:06











  • For exmaple implement a method called getCountUsers() there you could return count(self::getUser());

    – Fabian
    Nov 20 '18 at 13:09






  • 1





    Yes. Or you can create an new attribute on your Role entity called private $countUsers and use symfony.com/doc/current/doctrine/lifecycle_callbacks.html to set this value on postLoad

    – Fabian
    Nov 20 '18 at 13:15
















0















Using this Query:



$qb = $this->createQueryBuilder('r');
$qb->leftJoin('r.users', 'u')
->addSelect('count(u.id) as user_count')
->groupBy('r.id');


I get result-set:



Array
(
[0] => Array
(
[0] => Array
(
[id] => 45
[role_name] => ROLE_ADMIN
[description] => Admin roles
)

[user_count] => 1
)

[1] => Array
(
[0] => Array
(
[id] => 47
[role_name] => ROLE_OPERATOR
[description] => Operator role
)

[user_count] => 1
)

[2] => Array
(
[0] => Array
(
[id] => 48
[role_name] => ROLE_TEST
[description] => ROLE_TEST
)

[user_count] => 0
)

)


Is there a smart way of transforming all the properties the same level? As now a roles are in sub Array with 0 key.



Desired result:



Array
(
[0] => Array
(
[id] => 45
[role_name] => ROLE_ADMIN
[description] => Admin roles
[user_count] => 1
)

[1] => Array
(
[id] => 47
[role_name] => ROLE_OPERATOR
[description] => Operator role
[user_count] => 1
)

[2] => Array
(
[id] => 48
[role_name] => ROLE_TEST
[description] => ROLE_TEST
[user_count] => 0
)

)









share|improve this question























  • I think there is no chance to this, because the first select is the object itselfs and the second is an additionally select stmt. If user_count would be an attribute of your object this would work.

    – Fabian
    Nov 20 '18 at 13:00











  • But I think you can count the users on your $role object if you have an ->getUsers() method in your Role class?

    – Fabian
    Nov 20 '18 at 13:02











  • @Fabian, yes, there is Role::getUser() method in a entity. How do I add count to a result-set in this case?

    – Roman Newaza
    Nov 20 '18 at 13:06











  • For exmaple implement a method called getCountUsers() there you could return count(self::getUser());

    – Fabian
    Nov 20 '18 at 13:09






  • 1





    Yes. Or you can create an new attribute on your Role entity called private $countUsers and use symfony.com/doc/current/doctrine/lifecycle_callbacks.html to set this value on postLoad

    – Fabian
    Nov 20 '18 at 13:15














0












0








0








Using this Query:



$qb = $this->createQueryBuilder('r');
$qb->leftJoin('r.users', 'u')
->addSelect('count(u.id) as user_count')
->groupBy('r.id');


I get result-set:



Array
(
[0] => Array
(
[0] => Array
(
[id] => 45
[role_name] => ROLE_ADMIN
[description] => Admin roles
)

[user_count] => 1
)

[1] => Array
(
[0] => Array
(
[id] => 47
[role_name] => ROLE_OPERATOR
[description] => Operator role
)

[user_count] => 1
)

[2] => Array
(
[0] => Array
(
[id] => 48
[role_name] => ROLE_TEST
[description] => ROLE_TEST
)

[user_count] => 0
)

)


Is there a smart way of transforming all the properties the same level? As now a roles are in sub Array with 0 key.



Desired result:



Array
(
[0] => Array
(
[id] => 45
[role_name] => ROLE_ADMIN
[description] => Admin roles
[user_count] => 1
)

[1] => Array
(
[id] => 47
[role_name] => ROLE_OPERATOR
[description] => Operator role
[user_count] => 1
)

[2] => Array
(
[id] => 48
[role_name] => ROLE_TEST
[description] => ROLE_TEST
[user_count] => 0
)

)









share|improve this question














Using this Query:



$qb = $this->createQueryBuilder('r');
$qb->leftJoin('r.users', 'u')
->addSelect('count(u.id) as user_count')
->groupBy('r.id');


I get result-set:



Array
(
[0] => Array
(
[0] => Array
(
[id] => 45
[role_name] => ROLE_ADMIN
[description] => Admin roles
)

[user_count] => 1
)

[1] => Array
(
[0] => Array
(
[id] => 47
[role_name] => ROLE_OPERATOR
[description] => Operator role
)

[user_count] => 1
)

[2] => Array
(
[0] => Array
(
[id] => 48
[role_name] => ROLE_TEST
[description] => ROLE_TEST
)

[user_count] => 0
)

)


Is there a smart way of transforming all the properties the same level? As now a roles are in sub Array with 0 key.



Desired result:



Array
(
[0] => Array
(
[id] => 45
[role_name] => ROLE_ADMIN
[description] => Admin roles
[user_count] => 1
)

[1] => Array
(
[id] => 47
[role_name] => ROLE_OPERATOR
[description] => Operator role
[user_count] => 1
)

[2] => Array
(
[id] => 48
[role_name] => ROLE_TEST
[description] => ROLE_TEST
[user_count] => 0
)

)






php symfony doctrine






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 20 '18 at 12:57









Roman NewazaRoman Newaza

7,61584068




7,61584068













  • I think there is no chance to this, because the first select is the object itselfs and the second is an additionally select stmt. If user_count would be an attribute of your object this would work.

    – Fabian
    Nov 20 '18 at 13:00











  • But I think you can count the users on your $role object if you have an ->getUsers() method in your Role class?

    – Fabian
    Nov 20 '18 at 13:02











  • @Fabian, yes, there is Role::getUser() method in a entity. How do I add count to a result-set in this case?

    – Roman Newaza
    Nov 20 '18 at 13:06











  • For exmaple implement a method called getCountUsers() there you could return count(self::getUser());

    – Fabian
    Nov 20 '18 at 13:09






  • 1





    Yes. Or you can create an new attribute on your Role entity called private $countUsers and use symfony.com/doc/current/doctrine/lifecycle_callbacks.html to set this value on postLoad

    – Fabian
    Nov 20 '18 at 13:15



















  • I think there is no chance to this, because the first select is the object itselfs and the second is an additionally select stmt. If user_count would be an attribute of your object this would work.

    – Fabian
    Nov 20 '18 at 13:00











  • But I think you can count the users on your $role object if you have an ->getUsers() method in your Role class?

    – Fabian
    Nov 20 '18 at 13:02











  • @Fabian, yes, there is Role::getUser() method in a entity. How do I add count to a result-set in this case?

    – Roman Newaza
    Nov 20 '18 at 13:06











  • For exmaple implement a method called getCountUsers() there you could return count(self::getUser());

    – Fabian
    Nov 20 '18 at 13:09






  • 1





    Yes. Or you can create an new attribute on your Role entity called private $countUsers and use symfony.com/doc/current/doctrine/lifecycle_callbacks.html to set this value on postLoad

    – Fabian
    Nov 20 '18 at 13:15

















I think there is no chance to this, because the first select is the object itselfs and the second is an additionally select stmt. If user_count would be an attribute of your object this would work.

– Fabian
Nov 20 '18 at 13:00





I think there is no chance to this, because the first select is the object itselfs and the second is an additionally select stmt. If user_count would be an attribute of your object this would work.

– Fabian
Nov 20 '18 at 13:00













But I think you can count the users on your $role object if you have an ->getUsers() method in your Role class?

– Fabian
Nov 20 '18 at 13:02





But I think you can count the users on your $role object if you have an ->getUsers() method in your Role class?

– Fabian
Nov 20 '18 at 13:02













@Fabian, yes, there is Role::getUser() method in a entity. How do I add count to a result-set in this case?

– Roman Newaza
Nov 20 '18 at 13:06





@Fabian, yes, there is Role::getUser() method in a entity. How do I add count to a result-set in this case?

– Roman Newaza
Nov 20 '18 at 13:06













For exmaple implement a method called getCountUsers() there you could return count(self::getUser());

– Fabian
Nov 20 '18 at 13:09





For exmaple implement a method called getCountUsers() there you could return count(self::getUser());

– Fabian
Nov 20 '18 at 13:09




1




1





Yes. Or you can create an new attribute on your Role entity called private $countUsers and use symfony.com/doc/current/doctrine/lifecycle_callbacks.html to set this value on postLoad

– Fabian
Nov 20 '18 at 13:15





Yes. Or you can create an new attribute on your Role entity called private $countUsers and use symfony.com/doc/current/doctrine/lifecycle_callbacks.html to set this value on postLoad

– Fabian
Nov 20 '18 at 13:15












1 Answer
1






active

oldest

votes


















1














From my comment:




I think there is no chance to this, because the first select is the
object itselfs and the second is an additionally select stmt. If
user_count would be an attribute of your object this would work.




Instead you could add an new attribute called private $countUsers on your Role entity and set its value with Doctrine LifecycleCallbacks event PostLoad



Symfony implentation: https://symfony.com/doc/current/doctrine/lifecycle_callbacks.html



Doctrine events: https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/events.html#lifecycle-events






share|improve this answer


























  • Is it possible this $countUsers not to be added to Schema?

    – Roman Newaza
    Nov 20 '18 at 13:29











  • Yes don't put any relevant doctrine annotions above it

    – Fabian
    Nov 20 '18 at 13:30






  • 1





    Yes, got it. Thanks!

    – Roman Newaza
    Nov 20 '18 at 13:32











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%2f53393507%2fresult-data-structure-of-doctrine-join%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














From my comment:




I think there is no chance to this, because the first select is the
object itselfs and the second is an additionally select stmt. If
user_count would be an attribute of your object this would work.




Instead you could add an new attribute called private $countUsers on your Role entity and set its value with Doctrine LifecycleCallbacks event PostLoad



Symfony implentation: https://symfony.com/doc/current/doctrine/lifecycle_callbacks.html



Doctrine events: https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/events.html#lifecycle-events






share|improve this answer


























  • Is it possible this $countUsers not to be added to Schema?

    – Roman Newaza
    Nov 20 '18 at 13:29











  • Yes don't put any relevant doctrine annotions above it

    – Fabian
    Nov 20 '18 at 13:30






  • 1





    Yes, got it. Thanks!

    – Roman Newaza
    Nov 20 '18 at 13:32
















1














From my comment:




I think there is no chance to this, because the first select is the
object itselfs and the second is an additionally select stmt. If
user_count would be an attribute of your object this would work.




Instead you could add an new attribute called private $countUsers on your Role entity and set its value with Doctrine LifecycleCallbacks event PostLoad



Symfony implentation: https://symfony.com/doc/current/doctrine/lifecycle_callbacks.html



Doctrine events: https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/events.html#lifecycle-events






share|improve this answer


























  • Is it possible this $countUsers not to be added to Schema?

    – Roman Newaza
    Nov 20 '18 at 13:29











  • Yes don't put any relevant doctrine annotions above it

    – Fabian
    Nov 20 '18 at 13:30






  • 1





    Yes, got it. Thanks!

    – Roman Newaza
    Nov 20 '18 at 13:32














1












1








1







From my comment:




I think there is no chance to this, because the first select is the
object itselfs and the second is an additionally select stmt. If
user_count would be an attribute of your object this would work.




Instead you could add an new attribute called private $countUsers on your Role entity and set its value with Doctrine LifecycleCallbacks event PostLoad



Symfony implentation: https://symfony.com/doc/current/doctrine/lifecycle_callbacks.html



Doctrine events: https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/events.html#lifecycle-events






share|improve this answer















From my comment:




I think there is no chance to this, because the first select is the
object itselfs and the second is an additionally select stmt. If
user_count would be an attribute of your object this would work.




Instead you could add an new attribute called private $countUsers on your Role entity and set its value with Doctrine LifecycleCallbacks event PostLoad



Symfony implentation: https://symfony.com/doc/current/doctrine/lifecycle_callbacks.html



Doctrine events: https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/events.html#lifecycle-events







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 20 '18 at 13:27









Roman Newaza

7,61584068




7,61584068










answered Nov 20 '18 at 13:19









FabianFabian

70311023




70311023













  • Is it possible this $countUsers not to be added to Schema?

    – Roman Newaza
    Nov 20 '18 at 13:29











  • Yes don't put any relevant doctrine annotions above it

    – Fabian
    Nov 20 '18 at 13:30






  • 1





    Yes, got it. Thanks!

    – Roman Newaza
    Nov 20 '18 at 13:32



















  • Is it possible this $countUsers not to be added to Schema?

    – Roman Newaza
    Nov 20 '18 at 13:29











  • Yes don't put any relevant doctrine annotions above it

    – Fabian
    Nov 20 '18 at 13:30






  • 1





    Yes, got it. Thanks!

    – Roman Newaza
    Nov 20 '18 at 13:32

















Is it possible this $countUsers not to be added to Schema?

– Roman Newaza
Nov 20 '18 at 13:29





Is it possible this $countUsers not to be added to Schema?

– Roman Newaza
Nov 20 '18 at 13:29













Yes don't put any relevant doctrine annotions above it

– Fabian
Nov 20 '18 at 13:30





Yes don't put any relevant doctrine annotions above it

– Fabian
Nov 20 '18 at 13:30




1




1





Yes, got it. Thanks!

– Roman Newaza
Nov 20 '18 at 13:32





Yes, got it. Thanks!

– Roman Newaza
Nov 20 '18 at 13:32




















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%2f53393507%2fresult-data-structure-of-doctrine-join%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







這個網誌中的熱門文章

Academy of Television Arts & Sciences

L'Équipe

1995 France bombings