Extbase: Choose lazy or eager loading at runtime
Asked Answered
G

2

6

I have a domain object Foo that has an 1:n relation to a domain object Bar.

There are two major use cases where I need to get all foo's matching some criterion. In case A, I care about the bars attached to each foo, in case B, I don't. There are quite a lot of bars, so simply always loading the bars is not good for performance of case A. Similarly, not loading the bars eagerly will lead to an n+1 avalanche in case B. So neither tagging the realtion as @Lazy nor not tagging it is the correct choice.

Now, my question: Is it possible to tell the extbase persistence layer at query time whether to be lazy or eager? If yes, how? If no, is there another way in Extbase to avoid the n+1 problem (i.e. load all necessary bars and then hope that caching works when iterating through the foos)?

My last resort, of course, would be to load the foos with lazy loading, load the bars manually in a second query, and then manually set the relation.

Any suggestions?

Gorki answered 5/3, 2012 at 15:42 Comment(6)
I solved it by redesigning case B to work on the foos individually. Not nice, but better, and fast enough for the time being. I still would like how to achieve this.Gorki
Can you answer your own question and then mark ist as solved? It still pops up in the list of unsanswered [typo3] questions.Zeitgeist
Hm... I don't really think the question has been answered. I found a workaround for this particular problem, but the general case...Gorki
Alright. I understand your first comment now.Zeitgeist
How about doing a SQL query and then manually map rows to objects using PropertyMapper?Necrolatry
Doing a SQL query and return raw result, I mean.Necrolatry
G
2

I've been playing around with Extbase and delving into the internals in the last few months, and the upshot is this: It' impossible.

I suppose that closes this question, though not the way I'ld like.

Actually, even worse: Eager loading is not implemented at all, the @eager tag which according to the documentation sets eager loading for a relation is ignored.

Gorki answered 16/9, 2012 at 16:36 Comment(3)
For the last part: Eager loading is the default, hence there is no @eager Tag. You can just change this behaviour to lazy with the @lazy Tag.Gilberte
@Gilberte That would not make it possible to chose between lazy or eager behaviour at runtime, I'm afraid.Necrolatry
Yeah, you can't set it at runtime.Gilberte
L
0

what about leaving it lazy and converting it to an array when needed? (foo->bar->toArray())

Least answered 4/10, 2012 at 14:15 Comment(2)
What are the benefits of doing this?Phonoscope
That would still be lazy (extra DB query at usage time to get missing data, one query for each item) instead of eager (all necessary data is loaded from the db by the single, initial query)Gorki

© 2022 - 2024 — McMap. All rights reserved.