XF 2.1 Order by

AndrewSimm

Well-known member
In my widget I have that displays the author fullname if it exists or the username if the fullname does not exist.
Code:
<xf:if is="$author.fullname">
    <a href="{{ link('articles/author', $author) }}">{$author.fullname}</a>
<xf:else />
    <a href="{{ link('articles/author', $author) }}">{$author.username}</a>
</xf:if>

In my controller I would like to order by the fullname if it exist and if it doesn't then use the username. Thoughts?
PHP:
$authors = $finder->order('fullname','ASC')->limit($limit)->fetch();
 
Last edited:
In my widget I have that displays the author fullname if it exists or the username if the fullname does not exist.
Code:
<xf:if is="$author.fullname">
    <a href="{{ link('articles/author', $author) }}">{$author.fullname}</a>
<xf:else />
    <a href="{{ link('articles/author', $author) }}">{$author.username}</a>
</xf:if>

In my controller I would like to order by the fullname if it exist and if it doesn't then use the username. Thoughts?
PHP:
$authors = $finder->order('fullname','ASC')->limit($limit)->fetch();
use entity_structure in Code event listeners or extend class XF\Entity\User
 
use entity_structure in Code event listeners or extend class XF\Entity\User

I have already done that and username is stored in Author as well.

What I want to do is order by fullname if it exist and if it doesn't then order by username.

Example:
UsernameFullname
FatMatt22Matt Smith
Big Hammer
Cowboy JonesBobby Jones
ACoolDudePhillip Davis

I want the order to be:
Big Hammer
Bobby Jones
Matt Smith
Phillip Davis
 
I could be wrong, but I don't think the finder system is capable of doing complex sorting like that. You'll probably need to write a custom SQL query to get that result. I'm no SQL expert, but I think something along the lines of SELECT * FROM <table> WHERE <conditions> LIMIT <limit> ORDER BY CASE WHEN ISNULL(fullname) THEN username ELSE fullname END ASC; should do the trick.
 
I could be wrong, but I don't think the finder system is capable of doing complex sorting like that. You'll probably need to write a custom SQL query to get that result. I'm no SQL expert, but I think something along the lines of SELECT * FROM <table> WHERE <conditions> LIMIT <limit> ORDER BY CASE WHEN ISNULL(fullname) THEN username ELSE fullname END ASC; should do the trick.

That would do the trick, though I am trying to work through finder.

Working through the entity I added the following getter:
PHP:
        $structure->getters = [
            'author_name' => true
        ];


PHP:
    public function getAuthorName()
    {
        if($this->fullname)
        {
            return $this->fullname;
        } else {
            return $this->username;
        }
    }

}

The only issue is I can not order by the getter.
 
Last edited:
Back
Top Bottom