CodeIgniter 4模型:修改检索数据



我正在尝试使用CodeIgniter 4模型对数据库进行一些基本的CRUD操作。现在,我计划将mySQL DATETIME字段作为unix时间戳返回。
控制器:

<?php namespace AppControllersAPI;
use CodeIgniterRESTfulResourceController;
use CodeIgniterAPIResponseTrait;
use AppModelsConfigModel;
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 AppModels;
use CodeIgniterModel;
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实体,但不确定这是否是正确的方式。此外,我没有发现使用CI工具(即UNIX_TIMESTAMP(create_timestamp))更改单列SQL语句的任何可能性

最好的方法是什么?

使用过滤器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
}
}

我发现我走在正确的道路上。这个问题的关键是文档中的那些实体类。我添加了以下实体类:

<?php namespace AppEntities;
use CodeIgniterEntityEntity;
use CodeIgniterI18nTime;
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 AppModels;
use CodeIgniterModel;
class ConfigModel extends Model
{
// ...
// I think this is only needed for those 
// built-in CRUD function like findAll()
protected $returnType    = 'AppEntitiesConfig';
// ...
// Use the entity here
return $query->getResult('AppEntitiesConfig');
// ...
// 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
},
// ...
}

最新更新