How to prevent multiples querys for a entity relation

Hoffi

Well-known member
I am just trying to figure out how I can prevent many addiional querys.

I added a Relation to the post entity.

At the end I am accessing this Data in the Template, and everytime it's generated an addiional query. So far so good.

At the moment I am really puzzeling where to add the appropiate ->with Statement that all the data is fetched with the initial query?

I must oversee something, can someone give me the missing hint?
 
This:
PHP:
$finder = \XF::finder('XF:User');
$user = $finder->where('user_id', 1)->with('Admin')->fetchOne();
$lastLogin = $user->Admin->last_login; // no additional query
Instead of this:
PHP:
$finder = \XF::finder('XF:User');
$user = $finder->where('user_id', 1)->fetchOne();
$lastLogin = $user->Admin->last_login; // additional query
 
Oh, yes. Thats obvious and copied form the documentation.

I did not find the perfect place to enhance the default Query to fetch all Posts for the display where I can add my ->with('something') Code.

To be more specific: Which Event Lister I need to enhance?
 
I did not find the perfect place to enhance the default Query to fetch all Posts for the display where I can add my ->with('something') Code.
It's kinda difficult to give a generic answe as it depends on where you would need the data.

If it is just when displaying a thread I'd extend \XF\Repository\Post:findPostsForThreadView().

If you need it "everywhere" I'd modify the structure and add the relation to defaultWith, but keep in mind that this really means it is added everywhere a post entity is queried.
 
Last edited:
Back
Top Bottom