如何使函数在yii框架中的模型类中工作

时间:2015-11-11 20:38:19

标签: php yii

在我基于yii的示例项目中,我有一个名为gateway的模型,此模型有一个来自DB的变量,其名称为 $ time ,这是网关的创建时间 来自php time()函数
现在我想将此变量更改为可读形式以在视图中显示(而不是保存在DB中)为此我为此编写函数 setTime()并定义变量 $ readabletime < / strong>
我没有在控制器中调用函数 settime(),但在模型的 rules()中写入此行:        array('time','setTime')但它不起作用 我怎样才能在模型中使用函数?

这是我的模特

<?php


class UserGateway extends CActiveRecord
{
public $readabletime; 
/**
 * @return string the associated database table name
 */
public function tableName()
{
    return 'user_gateway';
}


public function rules()
{
    // NOTE: you should only define rules for those attributes that
    // will receive user inputs.
    return array(
        array('name, url, ip, time, userid, gatewaycategoryid', 'required'),
        array('time, status, userid, gatewaycategoryid, defaultgateway', 'numerical', 'integerOnly'=>true),
        array('name, url', 'length', 'max'=>120),
        array('ip', 'length', 'max'=>18),
        // The following rule is used by search().
        // @todo Please remove those attributes that should not be searched.
        array('id, name, url, ip, time, status, userid, gatewaycategoryid, defaultgateway', 'safe', 'on'=>'search'),
        array('time','setTime')
    );
}


public function setTime()
{
    $this->readabletime=date('m/d/Y H:i:s', $this->time);
}

}

这是我的观点:

<?php echo CHtml::link('Advanced Search','#',array('class'=>'search-    button')); ?>

<div class="search-form" style="display:none">

</div><!-- search-form -->

<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'UserAccountnumber-grid',

'dataProvider'=>$model->search(),

'filter'=>$model,
'columns'=>array(
    'name',
     'url',
    'ip',
    'readabletime',

    array(

        'class'=>'CButtonColumn',
        'buttons'=>array(
                'update' => array(
                'url'=>'Yii::app()->createUrl(\'User/UpdateGateway\',array(\'id\'=>$data[id]))'),
                'delete' => array(
                'url'=>'Yii::app()->createUrl(\'User/DeleteGateway\',array(\'id\'=>$data[id]))'

                        ),                            ),

                )
            )        


      )    



  );
  ?>

tnx:)

0 个答案:

没有答案