XF 2.2 KeyedBy finder/repository use?

Gobb

Member
Hi,

I am currently in the process of remaking some XF1 addons for XF2. The KeyedBy use is demonstrated in the MemberStats system in XF which is similar to the way I want to use it for my own addon. However, I only seem to be able to get it to return 1 result for each value in the KeyedBy column? Is this the way it's supposed to work?

In XF1 I did the same thing as how the alert dates were separated. Fetched the data and within a helper looped through it to assign a specific key name based upon the column, then through 2 foreach loops in the templates to split the data. Is this still the most appropriate way to do this in XF2, or is KeyedBy meant to be able to fetch more then a single row for each column of the same value?

Here is the current way I'm splitting it, which is basically how I did it in XF1:
Controller
PHP:
        $finder = \XF::finder('Gobb\List:List');
        $items = $finder->where('year', 2020)->fetch();
        
        $resultsData = [];
        
        foreach($items as $key => $value)
        {
            $resultsData[$value['type']][$key] = $value;
        }
        
        //\XF::dumpSimple($finder->getQuery());
        
        $viewParams = [
            //'memberStats' => $items,
            'resultsData' => $resultsData
        ];

Template:
Code:
<xf:foreach loop="$resultsData" key="$type" value="$data">
    <hr/>
    {$type}
    <hr/>
<xf:foreach loop="$data" value="$show">
{$show.title}   
</xf:foreach>
</xf:foreach>

Note that the "Type" could could be "Movie" or "TV", effectively grouping Movies with movies and TV with TV.

I don't particularly have an issue with how I've done it. I'm just not sure if I am meant to be able to do the same thing with just using KeyedBy in with the finder when fetching the data. Any insight would be appreciated.

Thanks.
 
keyedBy assumes that each key is only used once in your result set. If you want to group them, you can use groupBy after your fetch instead.

PHP:
$finder = \XF::finder('Gobb\List:List');
$items = $finder->where('year', 2020)->fetch();
$groupedItems = $items->groupBy('my_column');
 
keyedBy assumes that each key is only used once in your result set. If you want to group them, you can use groupBy after your fetch instead.

PHP:
$finder = \XF::finder('Gobb\List:List');
$items = $finder->where('year', 2020)->fetch();
$groupedItems = $items->groupBy('my_column');
Might be the best way to do it, thanks. :)
 
Top Bottom