Let's take a look for a sample query for retrieving Entity from DB
$query = $em->createQuery("
SELECT c
FROM Category c
WHERE c.id = 793
");
So, invoking "getResult" sends two queries to the Database (MySQL).:
$result = $query->getResult();
1. Querying the Category entity from DB
SELECT r0_.id AS id0 ... FROM categories r0_ WHERE (r0_.id = 793)
2. Second query select for a parent
SELECT t0.id AS id1 ... FROM categories t0 WHERE t0.id = 789
I assumed that second query will be run only when accessing
"Category::parent". But it's not so.
If I invoke "getArrayResult", I see that there is only one query.
SELECT r0_.id AS id0 ... FROM categories r0_ WHERE (r0_.id = 793)
But in that case array will be returned, so, to operate with object,
using getResult method will be preferred.
How to prevent sending two queries when I invoke getResult on Entity
with self referencing relationship.
$query = $em->createQuery("
SELECT c, parent
FROM Category c
LEFT JOIN c.parent parent
WHERE c.id = 793
");
$result = $query->getResult();
Then doctrine sends one query. Something like this:
SELECT c1.*, c2.* FROM Categories c1 LEFT JOIN Categories c2 ON
c1.parent_id = c2.id
but I still look for a solution how to get Entity from DB with self
referencing relationship withou additional query for a "parent".
May be anybody have any sugestions?
On 7 фев, 10:12, "sh.dime" <sharanov.dmi...@gmail.com> wrote:
> Let's take a look for a sample query for retrieving Entity from DB
> $query = $em->createQuery("
> SELECT c
> FROM Category c
> WHERE c.id = 793
> ");
> So, invoking "getResult" sends two queries to the Database (MySQL).:
> $result = $query->getResult();
> 1. Querying the Category entity from DB
> SELECT r0_.id AS id0 ... FROM categories r0_ WHERE (r0_.id = 793)
> 2. Second query select for a parent
> SELECT t0.id AS id1 ... FROM categories t0 WHERE t0.id = 789
> I assumed that second query will be run only when accessing
> "Category::parent". But it's not so.
> If I invoke "getArrayResult", I see that there is only one query.
> SELECT r0_.id AS id0 ... FROM categories r0_ WHERE (r0_.id = 793)
> But in that case array will be returned, so, to operate with object,
> using getResult method will be preferred.
> How to prevent sending two queries when I invoke getResult on Entity
> with self referencing relationship.
Hey, I think it's the default, but have you tried setting fetch="LAZY" in the ManyToOne annotation? By the way, have you looked into nested sets? If the data is not changing often they can be a pretty good alternative to self referencing columns (I havent used them since doctrine 1, but I think doctrine 2 did have support for them)
Cheers
Ezequiel Rabinovich.
Sent from my iPhone
On 08/02/2012, at 10:40, "sh.dime" <sharanov.dmi...@gmail.com> wrote:
> $query = $em->createQuery(" > SELECT c, parent > FROM Category c > LEFT JOIN c.parent parent > WHERE c.id = 793 > "); > $result = $query->getResult();
> Then doctrine sends one query. Something like this: > SELECT c1.*, c2.* FROM Categories c1 LEFT JOIN Categories c2 ON > c1.parent_id = c2.id
> but I still look for a solution how to get Entity from DB with self > referencing relationship withou additional query for a "parent". > May be anybody have any sugestions?
> On 7 фев, 10:12, "sh.dime" <sharanov.dmi...@gmail.com> wrote: >> Let's assume I have an entity with self referencing relationship. >> For example: >> /** >> * @ORM\Table(name="categories") >> * @ORM\Entity >> * >> */ >> class Category >> { >> /** >> * >> * @ORM\Column(name="id", type="integer", nullable=false) >> * @ORM\Id >> * @ORM\GeneratedValue >> */ >> protected $id;
>> Let's take a look for a sample query for retrieving Entity from DB
>> $query = $em->createQuery(" >> SELECT c >> FROM Category c >> WHERE c.id = 793 >> ");
>> So, invoking "getResult" sends two queries to the Database (MySQL).: >> $result = $query->getResult();
>> 1. Querying the Category entity from DB >> SELECT r0_.id AS id0 ... FROM categories r0_ WHERE (r0_.id = 793)
>> 2. Second query select for a parent >> SELECT t0.id AS id1 ... FROM categories t0 WHERE t0.id = 789
>> I assumed that second query will be run only when accessing >> "Category::parent". But it's not so.
>> If I invoke "getArrayResult", I see that there is only one query. >> SELECT r0_.id AS id0 ... FROM categories r0_ WHERE (r0_.id = 793)
>> But in that case array will be returned, so, to operate with object, >> using getResult method will be preferred. >> How to prevent sending two queries when I invoke getResult on Entity >> with self referencing relationship.
> -- > You received this message because you are subscribed to the Google Groups "doctrine-user" group. > To post to this group, send email to doctrine-user@googlegroups.com. > To unsubscribe from this group, send email to doctrine-user+unsubscribe@googlegroups.com. > For more options, visit this group at http://groups.google.com/group/doctrine-user?hl=en.
On Wed, Feb 8, 2012 at 12:42 PM, Tridem Lists <li...@tridemapps.com> wrote: > Is it possible? Let's say I have a collection of users with ids 1, 2, 3 - > Can I do something like:
> $myEntity->getUsers()->get(1)?
> Or do I have to use the find method each time? > Wouldn't this cause unwanted queries when my users have already been > loaded?
> -- > You received this message because you are subscribed to the Google Groups > "doctrine-user" group. > To post to this group, send email to doctrine-user@googlegroups.com. > To unsubscribe from this group, send email to > doctrine-user+unsubscribe@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/doctrine-user?hl=en.
Note that you should only use the indexBy parameter on columns that are unique. Placing it on non-unique columns will result in an incomplete collection.
-- Jasper N. Brouwer
On 08-02-2012, at 16:47, Ezequiel Rabinovich wrote:
> Hey, I think it's the default, but have you tried setting fetch="LAZY"
> in the ManyToOne annotation?
> By the way, have you looked into nested sets? If the data is not
> changing often they can be a pretty good alternative to self
> referencing columns (I havent used them since doctrine 1, but I think
> doctrine 2 did have support for them)
> Cheers
> Ezequiel Rabinovich.
> Sent from my iPhone
> On 08/02/2012, at 10:40, "sh.dime" <sharanov.dmi...@gmail.com> wrote:
> > if join table
> > $query = $em->createQuery("
> > SELECT c, parent
> > FROM Category c
> > LEFT JOIN c.parent parent
> > WHERE c.id = 793
> > ");
> > $result = $query->getResult();
> > Then doctrine sends one query. Something like this:
> > SELECT c1.*, c2.* FROM Categories c1 LEFT JOIN Categories c2 ON
> > c1.parent_id = c2.id
> > but I still look for a solution how to get Entity from DB with self
> > referencing relationship withou additional query for a "parent".
> > May be anybody have any sugestions?
> >> Let's take a look for a sample query for retrieving Entity from DB
> >> $query = $em->createQuery("
> >> SELECT c
> >> FROM Category c
> >> WHERE c.id = 793
> >> ");
> >> So, invoking "getResult" sends two queries to the Database (MySQL).:
> >> $result = $query->getResult();
> >> 1. Querying the Category entity from DB
> >> SELECT r0_.id AS id0 ... FROM categories r0_ WHERE (r0_.id = 793)
> >> 2. Second query select for a parent
> >> SELECT t0.id AS id1 ... FROM categories t0 WHERE t0.id = 789
> >> I assumed that second query will be run only when accessing
> >> "Category::parent". But it's not so.
> >> If I invoke "getArrayResult", I see that there is only one query.
> >> SELECT r0_.id AS id0 ... FROM categories r0_ WHERE (r0_.id = 793)
> >> But in that case array will be returned, so, to operate with object,
> >> using getResult method will be preferred.
> >> How to prevent sending two queries when I invoke getResult on Entity
> >> with self referencing relationship.
> > --
> > You received this message because you are subscribed to the Google Groups "doctrine-user" group.
> > To post to this group, send email to doctrine-user@googlegroups.com.
> > To unsubscribe from this group, send email to doctrine-user+unsubscribe@googlegroups.com.
> > For more options, visit this group athttp://groups.google.com/group/doctrine-user?hl=en.
I don't think your testcase really represents your initial question. In case you define the inheritance on ORM level, the Entities are not the same anymore, so this is no longer a self-reference...
Still, your initial question is an interesting one, will also run some tests. On Feb 9, 2012 5:48 AM, "sh.dime" <sharanov.dmi...@gmail.com> wrote:
> Doctrine sends only one query to the DB, as expected.
> So, How do I get rid of the additional queries in the case when single > table inheritance mapping is used?
> On 8 фев, 17:35, Ezequiel Rabinovich <erabinov...@gmail.com> wrote: > > Hey, I think it's the default, but have you tried setting fetch="LAZY" > > in the ManyToOne annotation? > > By the way, have you looked into nested sets? If the data is not > > changing often they can be a pretty good alternative to self > > referencing columns (I havent used them since doctrine 1, but I think > > doctrine 2 did have support for them)
> > Cheers
> > Ezequiel Rabinovich.
> > Sent from my iPhone
> > On 08/02/2012, at 10:40, "sh.dime" <sharanov.dmi...@gmail.com> wrote:
> > > Then doctrine sends one query. Something like this: > > > SELECT c1.*, c2.* FROM Categories c1 LEFT JOIN Categories c2 ON > > > c1.parent_id = c2.id
> > > but I still look for a solution how to get Entity from DB with self > > > referencing relationship withou additional query for a "parent". > > > May be anybody have any sugestions?
> > >> Let's take a look for a sample query for retrieving Entity from DB
> > >> $query = $em->createQuery(" > > >> SELECT c > > >> FROM Category c > > >> WHERE c.id = 793 > > >> ");
> > >> So, invoking "getResult" sends two queries to the Database (MySQL).: > > >> $result = $query->getResult();
> > >> 1. Querying the Category entity from DB > > >> SELECT r0_.id AS id0 ... FROM categories r0_ WHERE (r0_.id = 793)
> > >> 2. Second query select for a parent > > >> SELECT t0.id AS id1 ... FROM categories t0 WHERE t0.id = 789
> > >> I assumed that second query will be run only when accessing > > >> "Category::parent". But it's not so.
> > >> If I invoke "getArrayResult", I see that there is only one query. > > >> SELECT r0_.id AS id0 ... FROM categories r0_ WHERE (r0_.id = 793)
> > >> But in that case array will be returned, so, to operate with object, > > >> using getResult method will be preferred. > > >> How to prevent sending two queries when I invoke getResult on Entity > > >> with self referencing relationship.
> > > -- > > > You received this message because you are subscribed to the Google > Groups "doctrine-user" group. > > > To post to this group, send email to doctrine-user@googlegroups.com. > > > To unsubscribe from this group, send email to > doctrine-user+unsubscribe@googlegroups.com. > > > For more options, visit this group athttp:// > groups.google.com/group/doctrine-user?hl=en.
> -- > You received this message because you are subscribed to the Google Groups > "doctrine-user" group. > To post to this group, send email to doctrine-user@googlegroups.com. > To unsubscribe from this group, send email to > doctrine-user+unsubscribe@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/doctrine-user?hl=en.
- Entity CategoryAbstract, which is extend by two Entities using Class Table Inheritance (JOINED): - Entity ProductCategory - Entity SupplierCategory
At the 'child' Entities, both a 'parentCategory' and a 'subCategories' property are defined, which have the annotations that realize a self-reference association.
When using $entityManager->find('Entity\Category\ProductCategory', $id);
This generates the following SQL:
SELECT t1.id AS id2, t1.description AS description3, t1.name AS name4, t0.parent_category_id AS parent_category_id5, t1.discriminator FROM category_product_type t0 INNER JOIN category t1 ON t0.id = t1.id WHERE t1.id = ?
Which seems correct to me, so maybe you should prevent typing DQL and have the EntityManager resolve it for you?
Cheers
PS: the reason I chose using Class Table Inheritance is because the ProductCategory and SupplierCategory have different characteristics / associations with other Entities
On 9 February 2012 08:22, Menno Holtkamp <menno.holtk...@gmail.com> wrote:
> I don't think your testcase really represents your initial question. In > case you define the inheritance on ORM level, the Entities are not the same > anymore, so this is no longer a self-reference...
> Still, your initial question is an interesting one, will also run some > tests. > On Feb 9, 2012 5:48 AM, "sh.dime" <sharanov.dmi...@gmail.com> wrote:
>> Thank for your reply, Ezequiel. >> I figured out that it happens when single table inheritance is set. So >> I find it behaviour very strange.
>> Doctrine sends only one query to the DB, as expected.
>> So, How do I get rid of the additional queries in the case when single >> table inheritance mapping is used?
>> On 8 фев, 17:35, Ezequiel Rabinovich <erabinov...@gmail.com> wrote: >> > Hey, I think it's the default, but have you tried setting fetch="LAZY" >> > in the ManyToOne annotation? >> > By the way, have you looked into nested sets? If the data is not >> > changing often they can be a pretty good alternative to self >> > referencing columns (I havent used them since doctrine 1, but I think >> > doctrine 2 did have support for them)
>> > Cheers
>> > Ezequiel Rabinovich.
>> > Sent from my iPhone
>> > On 08/02/2012, at 10:40, "sh.dime" <sharanov.dmi...@gmail.com> wrote:
>> > > Then doctrine sends one query. Something like this: >> > > SELECT c1.*, c2.* FROM Categories c1 LEFT JOIN Categories c2 ON >> > > c1.parent_id = c2.id
>> > > but I still look for a solution how to get Entity from DB with self >> > > referencing relationship withou additional query for a "parent". >> > > May be anybody have any sugestions?
>> > >> Let's take a look for a sample query for retrieving Entity from DB
>> > >> $query = $em->createQuery(" >> > >> SELECT c >> > >> FROM Category c >> > >> WHERE c.id = 793 >> > >> ");
>> > >> So, invoking "getResult" sends two queries to the Database (MySQL).: >> > >> $result = $query->getResult();
>> > >> 1. Querying the Category entity from DB >> > >> SELECT r0_.id AS id0 ... FROM categories r0_ WHERE (r0_.id = 793)
>> > >> 2. Second query select for a parent >> > >> SELECT t0.id AS id1 ... FROM categories t0 WHERE t0.id = 789
>> > >> I assumed that second query will be run only when accessing >> > >> "Category::parent". But it's not so.
>> > >> If I invoke "getArrayResult", I see that there is only one query. >> > >> SELECT r0_.id AS id0 ... FROM categories r0_ WHERE (r0_.id = 793)
>> > >> But in that case array will be returned, so, to operate with object, >> > >> using getResult method will be preferred. >> > >> How to prevent sending two queries when I invoke getResult on Entity >> > >> with self referencing relationship.
>> > > -- >> > > You received this message because you are subscribed to the Google >> Groups "doctrine-user" group. >> > > To post to this group, send email to doctrine-user@googlegroups.com. >> > > To unsubscribe from this group, send email to >> doctrine-user+unsubscribe@googlegroups.com. >> > > For more options, visit this group athttp:// >> groups.google.com/group/doctrine-user?hl=en.
>> -- >> You received this message because you are subscribed to the Google Groups >> "doctrine-user" group. >> To post to this group, send email to doctrine-user@googlegroups.com. >> To unsubscribe from this group, send email to >> doctrine-user+unsubscribe@googlegroups.com. >> For more options, visit this group at >> http://groups.google.com/group/doctrine-user?hl=en.
Is it possible to use indexBy on an OneToOne entity (f.e. user using the id of the user) instead of an entity column?
-----Ursprüngliche Nachricht----- Von: doctrine-user@googlegroups.com [mailto:doctrine-user@googlegroups.com] Im Auftrag von Jasper N. Brouwer Gesendet: Mittwoch, 8. Februar 2012 16:56 An: doctrine-user@googlegroups.com Betreff: Re: [doctrine-user] Get sinble entity from collection by identity
Note that you should only use the indexBy parameter on columns that are unique. Placing it on non-unique columns will result in an incomplete collection.
-- Jasper N. Brouwer
On 08-02-2012, at 16:47, Ezequiel Rabinovich wrote:
> You can use the indexBy parameter in the annotation to define a field from
the related object that's going to be used as the collection key.
> Cheers.
-- You received this message because you are subscribed to the Google Groups "doctrine-user" group. To post to this group, send email to doctrine-user@googlegroups.com. To unsubscribe from this group, send email to doctrine-user+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/doctrine-user?hl=en.
/** @Id @OneToOne */ as the primary key. However i really not recommend this for onetoone, especially not if you make it bidirectional, because then the "inverse" side has to always fetch the association.
> Is it possible to use indexBy on an OneToOne entity (f.e. user using the id > of the user) instead of an entity column?
> -----Ursprüngliche Nachricht----- > Von: doctrine-user@googlegroups.com [mailto:doctrine-user@googlegroups.com > ] > Im Auftrag von Jasper N. Brouwer > Gesendet: Mittwoch, 8. Februar 2012 16:56 > An: doctrine-user@googlegroups.com > Betreff: Re: [doctrine-user] Get sinble entity from collection by identity
> Note that you should only use the indexBy parameter on columns that are > unique. > Placing it on non-unique columns will result in an incomplete collection.
> -- > Jasper N. Brouwer
> On 08-02-2012, at 16:47, Ezequiel Rabinovich wrote:
> > You can use the indexBy parameter in the annotation to define a field > from > the related object that's going to be used as the collection key.
> > Cheers.
> -- > You received this message because you are subscribed to the Google Groups > "doctrine-user" group. > To post to this group, send email to doctrine-user@googlegroups.com. > To unsubscribe from this group, send email to > doctrine-user+unsubscribe@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/doctrine-user?hl=en.
> -- > You received this message because you are subscribed to the Google Groups > "doctrine-user" group. > To post to this group, send email to doctrine-user@googlegroups.com. > To unsubscribe from this group, send email to > doctrine-user+unsubscribe@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/doctrine-user?hl=en.