【发布时间】:2015-02-21 00:19:08
【问题描述】:
我已按照 Yii2 指南尝试使用 Country 控制器。我在名为area 的数据库中添加了一个字段,该字段将是国家/地区。在 Country 模型中,我使用了 afterFind 事件,如下所示:
public function afterFind()
{
if ($this->area == NULL){
//$this->area-> What Could I do here?!!
$this->area = 'N/A';
}
return true;
}
如果没有区域设置为N/A,我想更改区域的默认值area 字段是浮动的。它在更新视图中工作正常。但是,鉴于 actionView 它返回有关格式的错误。在该视图中,我对属性进行了如下格式化:
<?= DetailView::widget([
'model' => $model,
'attributes' => [
['attribute' => 'code','format' => 'raw','value' => "{$model->code} <img style=\"box-shadow:0 0 5px #0099ff\" src=\"flags/".strtolower($model->code).".png\" />"],
'name',
['attribute' => 'population', 'format' => 'decimal'],
['attribute' => 'area', 'format' => 'decimal'],
],
]) ?>
错误是: Invalid Parameter – yii\base\InvalidParamException
“N/A”不是数值。
我的问题是:如何从模型中访问字段格式?
【问题讨论】: