asg*_*eo1 3 oracle timestamp zend-framework zend-date
根据我的理解,在Zend Framework中处理日期的最佳方法是从数据库中选择它们作为Unix时间戳.
// SELECT UNIX_TIMESTAMP(my_datetime_column) FROM my_table
$date = new Zend_Date($unixtimestamp, Zend_Date::TIMESTAMP);
Run Code Online (Sandbox Code Playgroud)
我认为在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);
Run Code Online (Sandbox Code Playgroud)
您将对任何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');
}
Run Code Online (Sandbox Code Playgroud)
我希望我的模型返回$结果,其中时间戳已经转换为Zend_Date.我不想在每个数据访问函数中编写一个循环来为我做这个.
所以要明白我的实际问题:
有没有人知道Zend_Db的一种方法,在结果集上设置某种后处理,从而自动将时间戳转换为Zend_Date对象?
我遇到过我想要这样做的场景.这是我用过的解决方案:
这是我在项目中使用的扩展行类的一个简化版本:
/**
* @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);
}
}
Run Code Online (Sandbox Code Playgroud)
对于我们的各个表类,我们重写这些函数:
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;
}
}
Run Code Online (Sandbox Code Playgroud)
现在,每次访问该字段时创建一个新的Zend_Date对象都有一些开销,所以在我们的类中,我们采取额外的措施来缓存日期对象等,但我不希望这会妨碍显示你解决方案.
归档时间: |
|
查看次数: |
1624 次 |
最近记录: |