oracle - 自动从数据库中选择日期作为 Zend_Date 对象

标签 oracle zend-framework timestamp zend-date

据我所知,在 Zend Framework 中处理日期的最佳方法是从数据库中选择它们作为 Unix 时间戳。

Quick Creation of Dates from Database Date Values

// SELECT UNIX_TIMESTAMP(my_datetime_column) FROM my_table
$date = new Zend_Date($unixtimestamp, Zend_Date::TIMESTAMP);

我认为在 Oracle 中实际上没有简单的方法来选择日期作为 Unix 时间戳或 ISO-8601 格式是一件痛苦的事情——Zend_Date 最了解这两种格式。

但我确实在 PL/SQL 中编写了一个函数来选择日期作为 unix 时间戳,所以我现在可以实际执行此操作。

使用 Zend_Db_Expr,我现在可以选择我的日期作为 Unix 时间戳:

$select = $db->select()
             ->from(array('p' => 'products'),
                    array(
                        'product_id',
                        'product_date' => new Zend_Db_Expr('toUnixTimestamp(product_date)')
                    )
               );

$results = $db->fetchAll($select);

您可以对任何 RDMS 使用类似的查询 - 大多数都具有时间戳函数。

我觉得这很烦人,因为现在我必须遍历 $results 以手动将时间戳转换为 Zend_Date 对象:

foreach($results as $result){
    $productDate = new Zend_Date($result['product_date'], Zend_Date::TIMESTAMP);
    echo $productDate->toString('dd/MMM/yyyy HH:mm:ss');
}

我希望我的模型返回时间戳已转换为 Zend_Date 的 $results。我不想在每个数据访问函数中都编写一个循环来为我执行此操作。

那么进入我实际问题的重点:

有谁知道使用 Zend_Db 对结果集设置某种后处理的方法,从而自动将时间戳转换为 Zend_Date 对象?

最佳答案

我曾遇到过想要这样做的场景。这是我用过的解决方案:

  • 为 Zend_Db_Table_Row 创建了一个扩展行类并重载了 __get() 和 __set() super 方法
  • 在我想使用日期对象的特定类/表中,创建了适当的方法来完成繁重的工作

这是我在我的项目中使用的扩展行类的简化版本:

/**
 * @category   FireUp
 * @package    FireUp_Db
 * @copyright  Copyright (c) 2007-2009 Fire Up Media, Inc. (http://www.fireup.net)
 * @license    http://dev.fireup.net/license/mit     MIT License
 * @uses       Zend_Db_Table_Row
 */
class FireUp_Db_Table_Row extends Zend_Db_Table_Row_Abstract
{
    /**
     * Retrieve row field value
     *
     * Checks for the existence of a special method to apply additional handling for the field data and calls the method if it exists
     *
     * @param  string $columnName The user-specified column name.
     * @return string             The corresponding column value.
     * @throws Zend_Db_Table_Row_Exception if the $columnName is not a column in the row.
     */
    public function __get($key)
    {
        $inflector = new Zend_Filter_Word_UnderscoreToCamelCase();

        $method = '_get' . $inflector->filter($key);

        if (method_exists($this, $method))
        {
            return $this->{$method}();
        }

        return parent::__get($key);
    }

    /**
     * Set row field value
     *
     * Checks for the existence of a special method to apply additional handling for the field data and calls the method if it exists
     *
     * @param  string $columnName The column key.
     * @param  mixed  $value      The value for the property.
     * @return void
     * @throws Zend_Db_Table_Row_Exception
     */
    public function __set($key, $value)
    {
        $inflector = new Zend_Filter_Word_UnderscoreToCamelCase();

        $method = '_set' . $inflector->filter($key);

        if (method_exists($this, $method))
        {
            return $this->{$method}($value);
        }

        return parent::__set($key, $value);
    }
}

对于我们单独的表类,我们重写函数:

class EntityRecord extends FireUp_Db_Table_Row
{
    protected function _getDateCreated()
    {
        return new Zend_Date($this->_data['date_created'], Zend_Date::ISO_8601);
    }

    protected function _setDateCreated($value)
    {
        if ($value instanceof Zend_Date)
        {
            $value = $value->toString('YYYY-MM-dd HH:mm:ss');
        }

        $this->_data['date_created'] = $value;
        $this->_modifiedFields['date_created'] = true;
    }
}

现在,每次访问该字段时创建一个新的 Zend_Date 对象会产生一些开销,因此在我们的类中,我们采取额外的措施来缓存日期对象等,但我不希望它妨碍向您展示解决方案。

关于oracle - 自动从数据库中选择日期作为 Zend_Date 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1647581/

相关文章:

oracle - 在 Oracle 中以毫秒为单位的字符串

zend-framework - 使用 Zend_Feed 创建的 RSS Feed - firefox 提示下载不显示为 RSS

php - Zend Form - 如何在子表单元素上设置值?

MySQL:选择语句并有选择地检索数据

mysql - 如何将数据库中的 Unix 时间戳数据更改为 MySql 时间戳?

python - Sqlalchemy - 如何在 Oracle 数据库中指定所有者

java - 使用 hibernate 5 和 c3po 连接池在 java 应用程序中设置 vsession.program

java - HSQLDB 中的 Oracle NUMBER(精度,小数位数)

zend-framework - 在 View 中使用 Zend_Acl 来显示/隐藏部分 View 的方法是什么

php - 使用 PHP 更新时,PostgreSQL 时间戳字段的格式不相同