【问题标题】:CodeIgniter 4 Model: Modify retrieved dataCodeIgniter 4 模型:修改检索到的数据
【发布时间】:2021-11-16 20:37:20
【问题描述】:

我正在尝试使用 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)

最好的方法是什么?

【问题讨论】:

    标签: php mysql codeigniter codeigniter-4


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

      <?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
          },
          // ...
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-10-17
        • 2016-04-21
        • 2014-10-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多