dongxiang3648 2012-03-27 17:51
浏览 15
已采纳

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

I wanted to specify the output of a field from within my model so I added a date key to my $_schema:

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()).

I want my base model to format any field that has the date key for output. I thought the best place to do that would be right after a 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.

What am I doing wrong? How do I alter this so that simply doing <?= $tag->created; ?> in my view will format the date the way I want? 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.

  • 写回答

2条回答 默认 最新

  • doukui7574 2012-03-27 23:46
    关注

    Regardless of a small fix for your after find filter, I would do it differently.

    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 ?

    An other way is to add an instance method in your model (or a BaseModel), called 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 ...

    A helper seems cleaner as we are talking about presenting data in your views.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记