Home > Software engineering >  Model class can not run in Codeigniter 4
Model class can not run in Codeigniter 4

Time:02-03

I am creating model and I want to access method in controller, but query gives error:

mysqli_sql_exception #1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ':0: AND 1 = :1: LIMIT 1' at line 3

What is mistake in my code?

<?php 
namespace App\Models;
    
use CodeIgniter\Model;
class UserModel extends Model{

    protected $table='users';
    protected $allowedFields=['employee_name','employee_email','employee_phone','employee_dept','employee_grade','employee_password','employee_cpassword','emp_image'];
    public $db;

    public function __construct()
    {
        $this->db = \Config\Database::connect();
    }

    public function getEmployee($id=false)
    {
        if($id==null)
        {
            return $this->findAll();
        }
        return $this->where(['id',$id])->first();
    }
}

Controller:

public function edit($id) {
    $model = model(UserModel::class);
    $data['emp']=$model->getEmployee($id);

    print_r($data['emp']);
    exit();
}

Routes:

$routes->get('/edit/(:num)','Admin::edit/$1');

CodePudding user response:

Change from:

return $this->where(['id',$id])->first();

to

return $this->where('id', $id)->first();

or

return $this->where(['id' => $id])->first();

Take a look at the document here

  •  Tags:  
  • Related