CodeIgniter Forums
Where does the getRecent() method originate from? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: General (https://forum.codeigniter.com/forumdisplay.php?fid=1)
+--- Forum: Lounge (https://forum.codeigniter.com/forumdisplay.php?fid=3)
+--- Thread: Where does the getRecent() method originate from? (/showthread.php?tid=88190)

Pages: 1 2


Where does the getRecent() method originate from? - sammyskills - 08-06-2023

From the docs,

PHP Code:
<?php

// app/Cells/RecentPostsCell.php

namespace App\Cells;

use 
CodeIgniter\View\Cells\Cell;

class 
RecentPostsCell extends Cell
{
    protected $posts;

    public function mount(?int $categoryId)
    {
        $this->posts model('PostModel')
            ->when(
                $categoryId,
                static fn ($query$categoryId) => $query->where('category_id'$categoryId)
            )
            ->getRecent();
    }


Where does the getRecent() method come from?


RE: Where does the getRecent() method originate from? - ozornick - 08-06-2023

It looks like a bug. Models don't have when(), getRecent() methods


RE: Where does the getRecent() method originate from? - kenjis - 08-06-2023

Query Builder has when().
See https://codeigniter4.github.io/CodeIgniter4/database/query_builder.html#builder-when

The getRecent() seems to be a custom method in PostModel.


RE: Where does the getRecent() method originate from? - sammyskills - 08-07-2023

Quote:The getRecent() seems to be a custom method in PostModel.

Since it wasn't stated clearly that it is a custom method, one will begin to wonder (just like me) where the method comes from.

I suggest that the docs should stick with non-custom methods/functions. WDYT?


RE: Where does the getRecent() method originate from? - kenjis - 08-07-2023

Indeed, it is preferable to have the code work as it is as much as possible.


RE: Where does the getRecent() method originate from? - ozornick - 08-07-2023

Does the model return Query Builder directly? VS Code does not have type hints


RE: Where does the getRecent() method originate from? - sammyskills - 08-07-2023

(08-07-2023, 10:55 AM)ozornick Wrote: Does the model return Query Builder directly? VS Code does not have type hints

You will have to append the builder method to the model like so:
PHP Code:
$model model(YourModel::class);
$model->builder()->when(); 

With that, it will be type-hinted by VS Code.


RE: Where does the getRecent() method originate from? - ozornick - 08-07-2023

Yes, i known. We are talking about an example in documentation.
$this->post = model('PostModel')->when() there is no autocomplete because it is not a builder


RE: Where does the getRecent() method originate from? - sammyskills - 08-09-2023

Oh, I get you. For that to work, the method will need to be added to the system/Model's class phpdoc @method keyword.
WDYT @kenjis ?

Meanwhile, I submitted a PR for the view cell docs: https://github.com/codeigniter4/CodeIgniter4/pull/7805


RE: Where does the getRecent() method originate from? - kenjis - 08-09-2023

(08-09-2023, 06:01 PM)sammyskills Wrote: Oh, I get you. For that to work, the method will need to be added to the system/Model's class phpdoc @method keyword.
WDYT @kenjis ?

Yes, if a Query Builder method returns $this and the method can be used in Models without problems,
we can add to the system/Model's class phpdoc @method keyword.
Adding it is fine by me.