错误调用成员函数 getPccMethod() on null CodeIgniter 4

Error Call to a member function getPccMethod() on null CodeIgniter 4

我想创建 2 个 JOIN 表 mysql 并出现错误。 Bengkel_model.php 上的函数引起的错误,但我不知道,因为我是 CodeIgniter 的新手。

我的控制器Bengkel.php

<?php namespace App\Controllers;

use CodeIgniter\Controller;
use App\Models\Bengkel_model;

class Bengkel extends Controller
{  

    public function index()
    {
        $model = new Bengkel_model();
        $data['bengkel'] = $model->getBengkel();
        echo view('bengkel_view',$data);
    }

    public function add_new()
    {
        echo view('add_bengkel_view');
    }

    public function save()
    {
        $model = new Bengkel_model();
        $data = array(
            'nmBengkel' => $this->request->getPost('nmBengkel'),
            'rtBengkel' => $this->request->getPost('rtBengkel')
        );
        $model->saveBengkel($data);
        return redirect()->to('/bengkel');
    }

    function edit($id)
    {
        $model = new Bengkel_model();
        $data['bengkel'] = $model->getBengkel($id)->getRow();
        echo view('edit_bengkel_view', $data);
    }

    public function update()
    {
        $model = new Bengkel_model();
        $id = $this->request->getPost('idBengkel');
        $data = array(
            'nmBengkel' => $this->request->getPost('nmBengkel'),
            'rtBengkel' => $this->request->getPost('rtBengkel'),
        );
        $model->updateBengkel($data, $id);
        return redirect()->to('/bengkel');
    }

    public function delete($id)
    {
        $model = new Bengkel_model();
        $model->deleteBengkel($id);
        return redirect()->to('/bengkel');
    }

    public function pcc_method()
    { 
        $data = $this->Bengkel_model->getPccMethod();
    }

}

我已经尝试在 Controller 上添加 Constructor,但它仍然出错。我是 CodeIgniter 的新手。

我的模型Bengkel_model.php

<?php namespace App\Models;
use CodeIgniter\Model;

class Bengkel_model extends Model
{
    protected $table = 'bengkel';

    public function getBengkel($id = false)
    {
        if($id === false){
            return $this->findAll();
        }
        else{
            return $this->getWhere(['idBengkel' => $id]);
        }   
    }

    public function getPccMethod()
    {
        $response = array();

        $this->db->select('*');
        $this->db->from('rating');
        $this->db->join('user','user.idUser = rating.idUser');
        $query = $this->db->get(); 
        $response = $query->result_array();
        if($query->num_rows() != 0)
        {
            return $response;
        }
        else
        {
            return false;
        }                   
    }

    public function saveBengkel($data)
    {
        $query = $this->db->table($this->table)->insert($data);
        return $query;
    }

    public function updateBengkel($data, $id)
    {
        $query = $this->db->table($this->table)->update($data, array('idBengkel' => $id));
        return $query;
    }

    public function deleteBengkel($id)
    {
        $query = $this->db->table($this->table)->delete(array('idBengkel' => $id));
        return $query;
    } 

}

任何人都可以帮助我。我很沮丧。抱歉我的英语不好。

像你模型的其他调用一样

public function pcc_method() 
    {
        $model = new Bengkel_model();
        $data = $model->getPccMethod();
    }

或者,如果您想避免在控制器的任何地方创建模型,请将其创建为控制器的属性。

class Bengkel extends Controller
{  
    protected $model = new Bengkel_model();
    // ...
    public function pcc_method()
    { 
        $data = $this->model->getPccMethod();
    }
}

如果您采用第二个选项,请不要忘记在您的控制器中使用 $this->model 而不是 $model 来调用您的模型。