Result Data structure of Doctrine Join
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
|
show 2 more comments
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
I think there is no chance to this, because the first select is the object itselfs and the second is an additionally select stmt. Ifuser_countwould be an attribute of your object this would work.
– Fabian
Nov 20 '18 at 13:00
But I think you can count theuserson your$roleobject if you have an->getUsers()method in yourRoleclass?
– Fabian
Nov 20 '18 at 13:02
@Fabian, yes, there isRole::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 calledgetCountUsers()there you couldreturn count(self::getUser());
– Fabian
Nov 20 '18 at 13:09
1
Yes. Or you can create an new attribute on your Role entity calledprivate $countUsersand use symfony.com/doc/current/doctrine/lifecycle_callbacks.html to set this value onpostLoad
– Fabian
Nov 20 '18 at 13:15
|
show 2 more comments
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
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
php symfony doctrine
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. Ifuser_countwould be an attribute of your object this would work.
– Fabian
Nov 20 '18 at 13:00
But I think you can count theuserson your$roleobject if you have an->getUsers()method in yourRoleclass?
– Fabian
Nov 20 '18 at 13:02
@Fabian, yes, there isRole::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 calledgetCountUsers()there you couldreturn count(self::getUser());
– Fabian
Nov 20 '18 at 13:09
1
Yes. Or you can create an new attribute on your Role entity calledprivate $countUsersand use symfony.com/doc/current/doctrine/lifecycle_callbacks.html to set this value onpostLoad
– Fabian
Nov 20 '18 at 13:15
|
show 2 more comments
I think there is no chance to this, because the first select is the object itselfs and the second is an additionally select stmt. Ifuser_countwould be an attribute of your object this would work.
– Fabian
Nov 20 '18 at 13:00
But I think you can count theuserson your$roleobject if you have an->getUsers()method in yourRoleclass?
– Fabian
Nov 20 '18 at 13:02
@Fabian, yes, there isRole::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 calledgetCountUsers()there you couldreturn count(self::getUser());
– Fabian
Nov 20 '18 at 13:09
1
Yes. Or you can create an new attribute on your Role entity calledprivate $countUsersand use symfony.com/doc/current/doctrine/lifecycle_callbacks.html to set this value onpostLoad
– 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
|
show 2 more comments
1 Answer
1
active
oldest
votes
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
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
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
add a comment |
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
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
add a comment |
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
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
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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_countwould be an attribute of your object this would work.– Fabian
Nov 20 '18 at 13:00
But I think you can count the
userson your$roleobject if you have an->getUsers()method in yourRoleclass?– 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 couldreturn 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 $countUsersand use symfony.com/doc/current/doctrine/lifecycle_callbacks.html to set this value onpostLoad– Fabian
Nov 20 '18 at 13:15