简体   繁体   English

锂:在find()之后运行过滤器以格式化输出

[英]Lithium: Run filter after find() to format output

I wanted to specify the output of a field from within my model so I added a date key to my $_schema : 我想从模型中指定字段的输出,因此在$_schema添加了一个date键:

models/Tags.php

<?php
    protected $_schema = array(
        'id'       => array('type' => 'integer', 'key'  => 'primary'),
        'title'    => array('type' => 'string'),
        'created'  => array('type' => 'integer', 'date' => 'F jS, Y - g:i a'),
        'modified' => array('type' => 'integer')
    );
?>

I store my time as an unsigned integer in the db (output of time() ). 我将时间作为无符号整数存储在db中( time()输出)。

I want my base model to format any field that has the date key for output. 我希望我的基本模型格式化任何具有date键以供输出的字段。 I thought the best place to do that would be right after a find : 我认为最好的选择就是在find

extensions/data/Model.php

<?php
    static::applyFilter('find', function($self, $params, $chain) {
        $schema = $self::schema();
        $entity = $chain->next($self, $params, $chain);

        foreach ($schema as $field => $options) {
            if (array_key_exists('date', $options)) {
                //format as a date
                $params['data'][$field] = $entity->formatDate($field, $options['date']);
            }
        }
        return $entity;
    });


    public function formatdate($entity, $field, $format, $timezone = 'Asia/Colombo') {
        $dt = new \DateTime();
        $tz = new \DateTimeZone($timezone);
        $dt->setTimestamp($entity->$field);
        $dt->setTimezone($tz);
        return $dt->format($format);
    }

?>

This doesn't seem to be working. 这似乎不起作用。 When I execute a find all, this filter seems to get hit twice. 当我执行全部查找时,此过滤器似乎被击中了两次。 The first time, $entity contains a count() of the results and only on the second hit does it contain the Records object. 第一次, $entity包含结果的count() ,仅在第二次命中时,它才包含Records对象。

What am I doing wrong? 我究竟做错了什么? How do I alter this so that simply doing <?= $tag->created; ?> 我该如何改变它,以便简单地<?= $tag->created; ?> <?= $tag->created; ?> in my view will format the date the way I want? <?= $tag->created; ?>在我看来会以我想要的方式格式化日期吗? This, essentially, needs to be an 'after filter', of sorts. 从本质上讲,这需要是一种“后过滤器”。

EDIT 编辑

If I can find a way to access the current model entity object (not the full namespaced path, $self contains that), I can probably solve my problem. 如果可以找到访问当前模型实体对象的方法(不是完整的命名空间路径,$ self包含该路径),则可以解决我的问题。

Regardless of a small fix for your after find filter, I would do it differently. 不管您的after find过滤器有什么小修正,我都会以不同的方式进行。

Every time you'll do a find, you'll override your date format, even if you don't want to display it, but only do a business logic like comparing dates etc ... 每次进行查找时,即使您不想显示日期格式,也将覆盖日期格式,而只是进行诸如比较日期等的业务逻辑...

Since you want to format your output only in your views (we are not talking about formatting on the fly json responses for an API, etc.), why not using a helper method ? 由于您只想在视图中格式化输出(我们不是在谈论API的json响应动态格式化等),为什么不使用helper方法呢?

An other way is to add an instance method in your model (or a BaseModel), called created_at() . 另一种方法是在模型(或BaseModel)中添加一个实例方法,称为created_at() Then, you will call it from a view with <?= $entity->created_at() ?> You can still force a format fetched from your $_schema, or pass it as a param, etc ... 然后,您将使用<?= $entity->created_at() ?>从视图中调用它。您仍然可以强制从$ _schema提取格式,或者将其作为参数传递,等等。

A helper seems cleaner as we are talking about presenting data in your views. 当我们正在谈论在您的视图中呈现数据时,助手似乎更干净。

I'm reading the OP's problem as the find filter executes twice. 我正在阅读OP的问题,因为find过滤器执行两次。 If that's right, then why not just check to see if the $entity contains a recordset? 如果正确,那么为什么不检查一下$ entity是否包含记录集呢?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM