01、ModelBase.php代码如下:
<?php namespace App\Models; use CodeIgniter\Database\ConnectionInterface; use CodeIgniter\Model; use CodeIgniter\Validation\ValidationInterface; class ModelBase extends Model { var $Db; function __construct(ConnectionInterface $db = null, ValidationInterface $validation = null) { parent::__construct($db, $validation); //创建数据库连接 $this->Db = \Config\Database::connect(); } //-------------------------------------------------------------------- public function find($id = false) { if ($id === false) { return $this->findAll(); } return $this->where(['id' => $id])->first(); } public function date_insert($data) { return $this->insert($data); } public function date_update($data, $id) { return $this->update($id, $data); } public function date_delete($id) { return $this->delete($id); } }
02、Model_usertoken.php代码如下:
<?php namespace App\Models; class Model_usertoken extends ModelBase { protected $table = 'user_token'; protected $primaryKey = 'id'; protected $allowedFields = ['username', 'token', 'exp_time']; public function __construct() { parent::__construct(); } public function date_query($date) { $id = $date['id']; //sql语句 $sql = 'SELECT * FROM `user` WHERE `ID` IN ? '; //带参数条件查询 $rst = $this->db->query($sql, [$id] ); echo $this->db->getLastQuery(); return $rst->getResultArray(); } }
03、调用如下:
<?php namespace App\Controllers\Auth; use App\Controllers\BaseController; class Login extends baseController { protected $userModelToken; function __construct() { $this->Jwt = new Tx_jwt; $this->userModelToken = model('App\Models\Model_usertoken'); } //http://localhost/phmci4/public/index.php/auth/login/test001 public function test001() { $param = array( 'id' => [1], ); $data = $this->userModelToken->date_query($param); ShowMessage($data); } }
04、结构如下:
05、浏览器效果如下:
标签:function,return,CodeIgniter,public,039,date,封装,data,id From: https://www.cnblogs.com/tianpan2019/p/18390184