CodeIgniter 4 模型:修改检索到的数据
CodeIgniter 4 Model: Modify retrieved data
我正在尝试使用 CodeIgniter 4 模型对数据库进行一些基本的 CRUD 操作。现在我已经到了计划将 return 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 工具(即 UNIX_TIMESTAMP(create_timestamp)
)
更改单列的 SQL 语句
最好的方法是什么?
我发现我走的路是对的。这个问题的关键是文档中的实体 classes。我添加了以下实体 class:
<?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://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
}
}
我正在尝试使用 CodeIgniter 4 模型对数据库进行一些基本的 CRUD 操作。现在我已经到了计划将 return 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 工具(即 UNIX_TIMESTAMP(create_timestamp)
)
最好的方法是什么?
我发现我走的路是对的。这个问题的关键是文档中的实体 classes。我添加了以下实体 class:
<?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://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
}
}