模型 class 不能在 Codeigniter 4 中 运行
Model class can not run in Codeigniter 4
我正在创建模型,我想访问控制器中的方法,但查询给出错误:
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
我的代码有什么错误?
<?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();
}
}
控制器:
public function edit($id) {
$model = model(UserModel::class);
$data['emp']=$model->getEmployee($id);
print_r($data['emp']);
exit();
}
路线:
$routes->get('/edit/(:num)','Admin::edit/');
更改自:
return $this->where(['id',$id])->first();
到
return $this->where('id', $id)->first();
或
return $this->where(['id' => $id])->first();
看看文档here
我正在创建模型,我想访问控制器中的方法,但查询给出错误:
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
我的代码有什么错误?
<?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();
}
}
控制器:
public function edit($id) {
$model = model(UserModel::class);
$data['emp']=$model->getEmployee($id);
print_r($data['emp']);
exit();
}
路线:
$routes->get('/edit/(:num)','Admin::edit/');
更改自:
return $this->where(['id',$id])->first();
到
return $this->where('id', $id)->first();
或
return $this->where(['id' => $id])->first();
看看文档here