[英]how to make a function execute in model without calling in controller ,YII framework
In my yii based sample project I have a model named gateway and this model has a variable from DB with name $time that is a creation time for gateway that comes from php time() function.在我基于 yii 的示例项目中,我有一个名为 gateway 的模型,这个模型有一个来自 DB 的变量,名称为 $time ,它是来自 php time() 函数的网关的创建时间。
I want to change this variable to a readable form to show in view (not to save in DB) and for this I wrote a function setTime() and defined a variable $readabletime我想将此变量更改为可读形式以显示在视图中(而不是保存在数据库中),为此我编写了一个函数setTime()并定义了一个变量$readabletime
I didn't call function settime() in controller but in rules() of model我没有在控制器中调用函数settime()而是在模型的rules()
I wrote this line:我写了这一行:
array('time','setTime')
but it doesn,t work但它不起作用
How can I make a function work in model?如何使函数在模型中工作?
This is my model这是我的模型
<?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);
}
}
and this is my view:这是我的观点:
<?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]))'
), ),
)
)
)
);
?>
thank you all for answering谢谢大家的回答
1) Validation rules and their functions are only called on $model->validate()
. 1) 验证规则及其函数仅在
$model->validate()
上调用。
2) You've just hijacked Yii's native method for setting database properties without calling the parent, meaning your setTime()
function will be called when something like this is done: $model->time = time()
; 2) 您刚刚劫持了 Yii 的用于设置数据库属性的本机方法,而无需调用父级,这意味着您的
setTime()
函数将在执行以下操作时被调用: $model->time = time()
; meaning time
will never get set on the internal array of database attributes;这意味着
time
永远不会设置数据库属性的内部阵列上; meaning a time value will never be saved in the database.这意味着时间值永远不会保存在数据库中。 Conclusion: don't do that.
结论:不要那样做。
3) There are a couple ways you can accomplish this. 3) 有几种方法可以实现这一点。
i.一世。 Override the
afterFind()
( http://www.yiiframework.com/doc/api/1.1/CActiveRecord#afterFind-detail ) function, and set $readdabletime
equal to a formatted version of time.覆盖
afterFind()
( http://www.yiiframework.com/doc/api/1.1/CActiveRecord#afterFind-detail ) 函数,并将$readdabletime
设置$readdabletime
等于时间的格式化版本。 This will be called right after your model properties are loaded from the database (make sure to call parent::afterFind()
at the bottom of your function.这将在从数据库加载模型属性后立即调用(确保在函数底部调用
parent::afterFind()
。
ii. ii. Remove the line,
public $readdabletime;
删除这一行,
public $readdabletime;
, and add this function to your code instead: ,并将此函数添加到您的代码中:
public function getReaddabletime() {
return date('m/d/Y H:i:s', $this->time);
}
The formatted time will be accessible like this: $model->readdabletime
格式化的时间可以这样访问:
$model->readdabletime
In your model simply do在你的模型中简单地做
public function getTime(){
return date('m/d/Y H:i:s', $this->time);
}
Then, in CGridView然后,在 CGridView
'url',
'ip',
array(
'name' => 'Readable time',
'value' => $model->getTime()
),
...
You could just write afterFind
function in your model:您可以在模型中编写
afterFind
函数:
protected function afterFind()
{
// convert to readable time format while viewing
$this->readabletime = date('m/d/Y H:i:s', $this->time);
parent::afterFind();
}
This way wherever readabletime
is used, it will convert it to your desired format.这样,无论何时使用
readabletime
,它都会将其转换为您想要的格式。 CActiveRecord afterFind CActiveRecord afterFind
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.