XF 2.2 Finder limit noob question

Anatoliy

Well-known member
So I'm following "Building with XF2" video tutorials. I'm trying to build a simple addon that shows thread titles that are too long.
So far I made addon that shows all threads (2) from my new local xf installation. I have 2 questions.

1. How do I limit to say 20 result, so on my production forum it will not fetch thousands and thousands?
2. And how to incorporate in the code strlen()? Should I use it in my Controller, or in foreach loop in template?

Thanks in advance.

so far I have this

Code:
public function actionIndex(){
        $threadFinder = $this->finder('XF:Thread');
      
        $viewParams = [
            'mythreads' => $threadFinder->fetch()
        ];
        return $this->view('AV\Titles:Title\Index', 'av_titles_index', $viewParams);
    }
 
Solution
something like

Code:
$threadFinder = $this->finder('XF:Thread')
->where(strlen('XF:Thread.title'), '>', '70')
->limit(20);
Try:
PHP:
$finder = $this->finder('XF:Thread');
$expression = $finder->expression('CHAR_LENGTH(%s)', 'title');
$results = $finder->where($expression, '>=', 70)->fetch();

TickTackk

Well-known member
How do I limit to say 20 result, so on my production forum it will not fetch thousands and thousands?
Assuming you're going to have more than 20 records which means you're looking for pagination and in that case something like this should do the trick
PHP:
$page = 69;
$perPage = 420;
$results = $finder->limitByPage($page, $perPage)->fetch()

And how to incorporate in the code strlen()? Should I use it in my Controller, or in foreach loop in template?
You need to give some example on how you want it to be used as well.
 

Anatoliy

Well-known member
Assuming you're going to have more than 20 records which means you're looking for pagination and in that case something like this should do the trick
PHP:
$page = 69;
$perPage = 420;
$results = $finder->limitByPage($page, $perPage)->fetch()

Thank you! I'm afraid that adding pagination could be a little bit too complex for me at this stage. )
I tried
Code:
 $threadFinder = $this->finder('XF:Thread')
        ->limit(20);
and amazingly it works!

You need to give some example on how you want it to be used as well.
I created a template and a tub, as in videos. Now addon shows a list of 20 thread titles linked to the threads. I want it to find and list only 20 threads with thread title longer than 70 characters. So I could click the link, go to the thread, and edit the title.

something like

Code:
$threadFinder = $this->finder('XF:Thread')
        ->where(strlen('XF:Thread.title'), '>', '70')
        ->limit(20);
 

TickTackk

Well-known member
something like

Code:
$threadFinder = $this->finder('XF:Thread')
->where(strlen('XF:Thread.title'), '>', '70')
->limit(20);
Try:
PHP:
$finder = $this->finder('XF:Thread');
$expression = $finder->expression('CHAR_LENGTH(%s)', 'title');
$results = $finder->where($expression, '>=', 70)->fetch();
 
Solution

Anatoliy

Well-known member
Well, my addon shows total 2k threads with too long titles. It will take me forever to fix them manually. )
So I'm thinking about some kind of bulk quick fix. Say by replacing "Thread title | Board name" with "Thread title" (without "| Board name") for those threads. What would be the best approach? I'm thinking maybe custom thread field, and conditional in some template to add Board name only if that custom thread field is empty?

Any advice?
 
Top