如何在其对象中引用Zend_Db_Table_Row属性

时间:2012-03-25 16:09:08

标签: php zend-framework

我遇到了问题,我使用Zend_Db_Table_Row来建模我的应用程序。 我有一个名为value的money属性的对象。它在数据库中作为int存储。 我想要做的是,每次调用此属性时都返回Zend_Currency对象。像这样:

$prepayment = Zend_Db_Table_Row
$currency = $prepayment->value; // should fetch Zend_Currency instead of plain int.

我试图在Row类中访问$ this->值,但似乎无效。如何在Row对象中覆盖属性的getter?任何的想法?

1 个答案:

答案 0 :(得分:2)

这样做的一种方法是创建一个视图助手,在显示之前将你的int值转换为Zend_Currency:

class My_Helper_Currency extends Zend_View_Helper_Abstract
{

    public function currency($value, $locale = 'fr_FR') {
        $currency = new Zend_Currency($locale);

        $value= $value+ 0.00;//to convert it to float

        return $currency->toCurrency((float) $value);

    }
} 

并在您的视图中显示:

 echo $this->currency($my_value);