XF 2.2 finder->with() question

Anatoliy

Well-known member
I'm building an add-on that shows me a list of users who are not active for a week, but were active within a previous week. And their xf_user_options.

So I built a controller with 2 actions (index & details), and 2 templates. On index page I see a list of users, sorted by message_count, desc. When I click a user I get to the details page that shows details from xf_user_option : receive_admin_email, creation_watch_state, interaction_watch_state, email_on_conversation.

But it would be more useful if all that data would be in a list on the index page without a need to click and go for details to another page. I understand that it probably could be done with ->(with), but I didn't use it before and I'm not sure that I understood correctly how it works.

I tried finder xf:user ->with('xf:UserOption') but it looks like it has no relation. I thought about finder for UserOption with User, but then I don't know how to sort results by message_count, desc.

Please push me to the right direction.
 
->with is just a join to the defined entity.


in Xf/Entity/User, it already has a relation to the user_options entity:

Code:
'Option' => [
				'entity' => 'XF:UserOption',
				'type' => self::TO_ONE,
				'conditions' => 'user_id',
				'primary' => true
			],

So, you would use
Code:
->with('Option')
assuming, you are extending XF:User in your class and have a class extension registered.
 
Last edited:
sorting, you just put it in your repository:

Code:
 $finder = $this->finder('XF:User');
        $finder
            ->setDefaultOrder('message_count', 'ASC')		
	     ->with('Option')

you can prefix like RelationName.Fieldname if you need to be specific about which ambiguous field name you want. (Option.user_id for example)
 
Last edited:
you can prefix like EntityName.Fieldname if you need to be specific about which ambigious field name you want. (Option.user_id for example)
RelationName, not EntityName.

Often this is identical (or at least very similar like in this specific example), but that isn't necessarily the case.

Sorry for being somewhat pedantic, but such threads tend to be used as reference/guide for newbies who might get confused by such little things :)
 
Top Bottom