Symfony 4, Doctrine Innerjoin doesn't use junction table
I have two entities (entity1 and entity2) in a ManyToMany relation. All tables are working fine, if i just need the object from one entity.
The fields in the entities are marked as ManyToMany including the mappedBy and reversedBy attributes.
I now need a subset from entity1 based on a where clause on entity2.
In SQL it's simple.
select
a.field1,a.field2
from entity1 as a
left join entity1_entity2 as b on a.entity1_id=b.foreignKey1
left join entity2 as c on b.foreignKey2=c.entity2_id
where c.whereField = 'value'
and a.whereField = 'anotherValue'
I want to use the Repository from entity1 and tried the query by the createQueryBuilder.
$query = $this->createQueryBuilder('e1')
->distinct(true)
->innerJoin(Entity2::class, 'e2')
->where("e2.whereField2= :whereE2")
->andWhere('e1.whereField1= :whereE1')
->setParameter("whereE2", $whereE2)
->setParameter("whereE1", $whereE1);
The result looks like a full outer join. The dump of the DQL/SQL seems that no junction table is used. Why?
symfony doctrine inner-join
add a comment |
I have two entities (entity1 and entity2) in a ManyToMany relation. All tables are working fine, if i just need the object from one entity.
The fields in the entities are marked as ManyToMany including the mappedBy and reversedBy attributes.
I now need a subset from entity1 based on a where clause on entity2.
In SQL it's simple.
select
a.field1,a.field2
from entity1 as a
left join entity1_entity2 as b on a.entity1_id=b.foreignKey1
left join entity2 as c on b.foreignKey2=c.entity2_id
where c.whereField = 'value'
and a.whereField = 'anotherValue'
I want to use the Repository from entity1 and tried the query by the createQueryBuilder.
$query = $this->createQueryBuilder('e1')
->distinct(true)
->innerJoin(Entity2::class, 'e2')
->where("e2.whereField2= :whereE2")
->andWhere('e1.whereField1= :whereE1')
->setParameter("whereE2", $whereE2)
->setParameter("whereE1", $whereE1);
The result looks like a full outer join. The dump of the DQL/SQL seems that no junction table is used. Why?
symfony doctrine inner-join
add a comment |
I have two entities (entity1 and entity2) in a ManyToMany relation. All tables are working fine, if i just need the object from one entity.
The fields in the entities are marked as ManyToMany including the mappedBy and reversedBy attributes.
I now need a subset from entity1 based on a where clause on entity2.
In SQL it's simple.
select
a.field1,a.field2
from entity1 as a
left join entity1_entity2 as b on a.entity1_id=b.foreignKey1
left join entity2 as c on b.foreignKey2=c.entity2_id
where c.whereField = 'value'
and a.whereField = 'anotherValue'
I want to use the Repository from entity1 and tried the query by the createQueryBuilder.
$query = $this->createQueryBuilder('e1')
->distinct(true)
->innerJoin(Entity2::class, 'e2')
->where("e2.whereField2= :whereE2")
->andWhere('e1.whereField1= :whereE1')
->setParameter("whereE2", $whereE2)
->setParameter("whereE1", $whereE1);
The result looks like a full outer join. The dump of the DQL/SQL seems that no junction table is used. Why?
symfony doctrine inner-join
I have two entities (entity1 and entity2) in a ManyToMany relation. All tables are working fine, if i just need the object from one entity.
The fields in the entities are marked as ManyToMany including the mappedBy and reversedBy attributes.
I now need a subset from entity1 based on a where clause on entity2.
In SQL it's simple.
select
a.field1,a.field2
from entity1 as a
left join entity1_entity2 as b on a.entity1_id=b.foreignKey1
left join entity2 as c on b.foreignKey2=c.entity2_id
where c.whereField = 'value'
and a.whereField = 'anotherValue'
I want to use the Repository from entity1 and tried the query by the createQueryBuilder.
$query = $this->createQueryBuilder('e1')
->distinct(true)
->innerJoin(Entity2::class, 'e2')
->where("e2.whereField2= :whereE2")
->andWhere('e1.whereField1= :whereE1')
->setParameter("whereE2", $whereE2)
->setParameter("whereE1", $whereE1);
The result looks like a full outer join. The dump of the DQL/SQL seems that no junction table is used. Why?
symfony doctrine inner-join
symfony doctrine inner-join
asked Nov 21 '18 at 12:34
ulfilasulfilas
83
83
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
You donn' have to specify the Entity in the innerJoin clause. Doctrine can do it by itself and use the join table too.
For example, entity1 as a ManyToMany annotation on the secondEntities
property.
The queryBuilder may look like:
$query = $this->createQueryBuilder('e1')
->distinct(true)
->innerJoin('e1.secondEntities', 'e2')
->andWhere('e1.whereField1= :whereE1') // You can add additionnal conditions ;)
->setParameter("whereE1", $whereE1)
;
Note: You can use ON
join condition like: ->innerJoin('e1.secondEntities', 'e2', Join::WITH, 'CONDITION HERE')
-- old comment deleted -- Now i see and understand the difference from my DQL and your . Thanks!
– ulfilas
Nov 21 '18 at 13:09
If it helped you to resolve your problem. Don't forget to mark this answer as "answered" for future references ;)
– G1.3
Nov 21 '18 at 13:17
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%2f53412152%2fsymfony-4-doctrine-innerjoin-doesnt-use-junction-table%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
You donn' have to specify the Entity in the innerJoin clause. Doctrine can do it by itself and use the join table too.
For example, entity1 as a ManyToMany annotation on the secondEntities
property.
The queryBuilder may look like:
$query = $this->createQueryBuilder('e1')
->distinct(true)
->innerJoin('e1.secondEntities', 'e2')
->andWhere('e1.whereField1= :whereE1') // You can add additionnal conditions ;)
->setParameter("whereE1", $whereE1)
;
Note: You can use ON
join condition like: ->innerJoin('e1.secondEntities', 'e2', Join::WITH, 'CONDITION HERE')
-- old comment deleted -- Now i see and understand the difference from my DQL and your . Thanks!
– ulfilas
Nov 21 '18 at 13:09
If it helped you to resolve your problem. Don't forget to mark this answer as "answered" for future references ;)
– G1.3
Nov 21 '18 at 13:17
add a comment |
You donn' have to specify the Entity in the innerJoin clause. Doctrine can do it by itself and use the join table too.
For example, entity1 as a ManyToMany annotation on the secondEntities
property.
The queryBuilder may look like:
$query = $this->createQueryBuilder('e1')
->distinct(true)
->innerJoin('e1.secondEntities', 'e2')
->andWhere('e1.whereField1= :whereE1') // You can add additionnal conditions ;)
->setParameter("whereE1", $whereE1)
;
Note: You can use ON
join condition like: ->innerJoin('e1.secondEntities', 'e2', Join::WITH, 'CONDITION HERE')
-- old comment deleted -- Now i see and understand the difference from my DQL and your . Thanks!
– ulfilas
Nov 21 '18 at 13:09
If it helped you to resolve your problem. Don't forget to mark this answer as "answered" for future references ;)
– G1.3
Nov 21 '18 at 13:17
add a comment |
You donn' have to specify the Entity in the innerJoin clause. Doctrine can do it by itself and use the join table too.
For example, entity1 as a ManyToMany annotation on the secondEntities
property.
The queryBuilder may look like:
$query = $this->createQueryBuilder('e1')
->distinct(true)
->innerJoin('e1.secondEntities', 'e2')
->andWhere('e1.whereField1= :whereE1') // You can add additionnal conditions ;)
->setParameter("whereE1", $whereE1)
;
Note: You can use ON
join condition like: ->innerJoin('e1.secondEntities', 'e2', Join::WITH, 'CONDITION HERE')
You donn' have to specify the Entity in the innerJoin clause. Doctrine can do it by itself and use the join table too.
For example, entity1 as a ManyToMany annotation on the secondEntities
property.
The queryBuilder may look like:
$query = $this->createQueryBuilder('e1')
->distinct(true)
->innerJoin('e1.secondEntities', 'e2')
->andWhere('e1.whereField1= :whereE1') // You can add additionnal conditions ;)
->setParameter("whereE1", $whereE1)
;
Note: You can use ON
join condition like: ->innerJoin('e1.secondEntities', 'e2', Join::WITH, 'CONDITION HERE')
answered Nov 21 '18 at 13:00
G1.3G1.3
82619
82619
-- old comment deleted -- Now i see and understand the difference from my DQL and your . Thanks!
– ulfilas
Nov 21 '18 at 13:09
If it helped you to resolve your problem. Don't forget to mark this answer as "answered" for future references ;)
– G1.3
Nov 21 '18 at 13:17
add a comment |
-- old comment deleted -- Now i see and understand the difference from my DQL and your . Thanks!
– ulfilas
Nov 21 '18 at 13:09
If it helped you to resolve your problem. Don't forget to mark this answer as "answered" for future references ;)
– G1.3
Nov 21 '18 at 13:17
-- old comment deleted -- Now i see and understand the difference from my DQL and your . Thanks!
– ulfilas
Nov 21 '18 at 13:09
-- old comment deleted -- Now i see and understand the difference from my DQL and your . Thanks!
– ulfilas
Nov 21 '18 at 13:09
If it helped you to resolve your problem. Don't forget to mark this answer as "answered" for future references ;)
– G1.3
Nov 21 '18 at 13:17
If it helped you to resolve your problem. Don't forget to mark this answer as "answered" for future references ;)
– G1.3
Nov 21 '18 at 13:17
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%2f53412152%2fsymfony-4-doctrine-innerjoin-doesnt-use-junction-table%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