我正在嘗試使用 CodeIgniter 4 模型對資料庫進行一些基本的 CRUD 操作。現在,我計劃將 mySQL DATETIME 欄位作為 unix 時間戳回傳。到目前為止我得到了什么:
控制器:
<?php namespace App\Controllers\API;
use CodeIgniter\RESTful\ResourceController;
use CodeIgniter\API\ResponseTrait;
use App\Models\ConfigModel;
class Config extends ResourceController
{
use ResponseTrait;
public function index()
{
$model = new ConfigModel();
$default = NULL;
if (null !== $this->request->getVar('config_default')){
$default = filter_var($this->request->getVar('config_default'), FILTER_VALIDATE_BOOLEAN);
}
return $this->respond($model->getConfig($default));
}
模型:
<?php namespace App\Models;
use CodeIgniter\Model;
class ConfigModel extends Model
{
protected $table = 'configuration';
protected $primaryKey = 'id';
protected $allowedFields = ['name','value','is_default'];
protected function getCurrentConfig($item = NULL){
if (isset($item)){
$query = $this->query('SELECT * FROM configuration WHERE id IN (SELECT MAX(id) FROM configuration WHERE name = '.$this->escape($item).')');
return $query->getResult();
} else {
$query = $this->query('SELECT * FROM configuration WHERE id IN (SELECT MAX(id) FROM configuration GROUP BY name)');
return $query->getResult();
}
}
protected function getDefaultConfig($item = NULL){
if (isset($item)){
return $this->where(['is_default' => 1, 'name' => $item])->findAll();
} else {
return $this->getWhere(['is_default' => 1])->getResult();
}
}
public function getConfig($default = FALSE){
if ($default) {
return $this->getDefaultConfig();
} else {
return $this->getCurrentConfig();
}
}
結果:
[
{
"id": "1",
"name": "ticket_number_format",
"value": "default",
"is_default": "1",
"create_timestamp": "2021-09-22 09:11:07" // need: 1632294667
},
{
"id": "3",
"name": "ticket_number_format_default_alphanumeric",
"value": "true",
"is_default": "1",
"create_timestamp": "2021-09-22 09:21:40" // need: 1632295300
},
...
]
我考慮過操作結果陣列,但對于我想要實作的目標來說似乎太復雜了。我對CodeIgniter Entities感到不滿,但不確定這是否是正確的方法。此外,我沒有發現任何可能使用 CI 工具更改單列的 SQL 陳述句(即UNIX_TIMESTAMP(create_timestamp))
最好的方法是什么?
uj5u.com熱心網友回復:
使用過濾器 https://codeigniter.com/user_guide/incoming/filters.html
class MyFilter implements FilterInterface
{
public function before(RequestInterface $request, $arguments = null)
{
// Do something here
}
public function after(RequestInterface $request, ResponseInterface $response, $arguments = null)
{
// Do something here
}
}
uj5u.com熱心網友回復:
我發現我走在正確的道路上。這個問題的關鍵是檔案中的那些物體類。我添加了以下物體類:
<?php namespace App\Entities;
use CodeIgniter\Entity\Entity;
use CodeIgniter\I18n\Time;
class Config extends Entity
{
public function getCreateTimestamp()
{
$this->attributes['create_timestamp'] = $this->mutateDate($this->attributes['create_timestamp']);
return $this->attributes['create_timestamp']->getTimestamp();
}
}
并修改了模型:
<?php namespace App\Models;
use CodeIgniter\Model;
class ConfigModel extends Model
{
// ...
// I think this is only needed for those
// built-in CRUD function like findAll()
protected $returnType = 'App\Entities\Config';
// ...
// Use the entity here
return $query->getResult('App\Entities\Config');
// ...
// But can be omitted here
return $this->where(['is_default' => 1, 'name' => $item])->findAll();
// ...
}
給出以下結果:
[
{
"id": "1",
"name": "ticket_number_format",
"value": "default",
"is_default": "1",
"create_timestamp": 1632319867
},
{
"id": "3",
"name": "ticket_number_format_default_alphanumeric",
"value": "true",
"is_default": "1",
"create_timestamp": 1632320500
},
// ...
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/315066.html
標籤:php mysql 代码点火器 codeigniter-4
下一篇:如何將陣列值加載為動態?
