Welcome Guest, Not a member yet? Register   Sign In
Where does the getRecent() method originate from?
#1

(This post was last modified: 08-06-2023, 08:23 AM by sammyskills.)

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?
Reply
#2

It looks like a bug. Models don't have when(), getRecent() methods
Simple CI 4 project for beginners codeigniter-expenses ( topic )
Reply
#3

Query Builder has when().
See https://codeigniter4.github.io/CodeIgnit...ilder-when

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

(This post was last modified: 08-07-2023, 12:04 AM by sammyskills.)

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?
Reply
#5

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

Does the model return Query Builder directly? VS Code does not have type hints
Simple CI 4 project for beginners codeigniter-expenses ( topic )
Reply
#7

(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.
Reply
#8

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
Simple CI 4 project for beginners codeigniter-expenses ( topic )
Reply
#9

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
Reply
#10

(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.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB