XF 2.0 Question about the template code xf:username user

AndyB

Well-known member
Hello,

I'm creating an add-on called Forum statistics and I would like to show the latest user in a widget.

My PHP code
PHP:
$finder = \XF::finder('XF:User');
$userLatest = $finder->order('register_date', 'DESC')->limit(1)->fetch();

My template code
Code:
<dl class="pairs pairs--justified">
    <dt>{{ phrase('latest_member') }}</dt>
    <dd><xf:username user="{$userLatest}" /></dd>
</dl>

The code I'm copying is from the widget_forum_statistics template:
Code:
<dl class="pairs pairs--justified">
    <dt>{{ phrase('latest_member') }}</dt>
    <dd><xf:username user="{$forumStatistics.latestUser}" /></dd>
</dl>

How come my object called $userLatest is not working? I would assume because I'm creating the object using the finder it would have all the data required.

Thank you for your help.
 
Last edited:
PHP:
$userLatest = $finder->order('register_date', 'DESC')->limit(1)->fetch();
The fetch() method will return an array collection -- an object which represents an array of entities.

You want the following instead:
PHP:
$userLatest = $finder->order('register_date', 'DESC')->fetchOne();
The fetchOne() method will return the first result as a single entity.
 
Top Bottom