setModel missing explanation

I’ve been trying for a couple days to create a custom model. The documentation for CodeIgniter 4 is completely missing.

I used to be able to use $this->db in codeigniter 3 while using GC but I can’t do that in CI4 so what is the equivalent?

Hello @gabe152006 and welcome to our forums :hugs: ,

For Codeigniter 4 you should use Codeigniter Models and call them in your function. $this->db was removed from Codeigniter 4 as it was an anti-pattern to be able to use the database from everywhere. Now you can use directly $db only from the Model it self as it is extending Model.

So for example instead of having:

public function getOne($id)
        $customer = parent::getOne($id);

        $this->db->select('COUNT(*) as count_orders');
        $this->db->where('customerNumber', $id);
        $customer['count_orders'] = $this->db->get('orders')->row()->count_orders;

        return $customer;

You will have something like this:

public function getOne($id)
        $customer = parent::getOne($id);

       $customerModel = new \App\Models\CustomerModel();
        $customer['count_orders'] = $customerModel->getCountOders($id);

        return $customer;

As Codeigniter 4 documentation is not that clear, I would like at the future to also create a video turorial for Codeigniter 4 models as part of my youtube tutorial series Codeigniter 4 Tutorial - YouTube , but for now I can copy-paste an example of a Model I am using for the blog of the website :slight_smile:

<?php namespace App\Models;

use CodeIgniter\Model;

class BlogModel extends Model
    public function getBlogPosts() {
        $blogPosts = $this->db->table('nw_blog_posts')->orderBy('insert_datetime', 'desc')->get()->getResult();

        foreach ($blogPosts as $num_row => $blogPost) {
            $blogPosts[$num_row]->insert_datetime_pretty = date_format(date_create($blogPost->insert_datetime), 'M d,Y');

        return $blogPosts;

Hi @johnny !

Thank you so much for providing this explanation.

I am a little confused in terms of how the GroceryCrud Enterprise Models work.

In CI4 I’m able to create my own models and call them out in my controllers and that’s it. In GC, do I need to use the specific functions listed in Model.php in \GroceryCrud\Core\Model and call the CI4 models within these functions?

I know GC uses Zend which is why I’m a bit confused here. Basically I’m trying to SUM all the values of a few columns while I group by some columns which I can do in CI4 just fine but how do I call this query into GC?

I have my custom CI4 Model that returns exactly what I want:

namespace App\Models;

use CodeIgniter\Model;

class ProjectionsModel extends Model 
	public function getSum() {
		$builder = $this->db->table('projections')
		->selectSum('sum_total', 'our_sum')
		->selectSum('sum_cleared', 'our_cleared')
		->selectSum('sum_planned_spend', 'our_planned')
		->selectSum('sum_projected', 'our_projected')
		$query = $builder->get()->getResult();
		return $query;

how do I use this model using the setModel() function of GroceryCrud Enterprise?

With Codeigniter 3 and GC V1.x I was able to filter my query before calling GroceryCrud without a model and it would work like:

$crud = new grocery_CRUD();
->select('sum(cost) as our_sum')
->order_by('our_sum', 'desc');
$output = $crud->render();
$this->load->view('WriteHereYourView.php', $output);

Now I can’t find how I can do something similar with or without a model.

Thank you so much

@johnny any insight?