Yii 框架 - 从其他模型中获取字段值 [英] Yii framework - picking up field value from other model
问题描述
我一直在努力解决这个问题,我有两个模型并用一个模型在 Cgridview 中显示数据,这个模型包含一些 id,其值在不同的表中
I have been struggling with this, i have two models and showing data in Cgridview with one model, this model contains some id's whose values are in different table
所以,我添加了
'value'=> 'TblAreaoflaw::model()->FindByPk($data->typeoflaw)->areaoflaw'
'value'=> 'TblAreaoflaw::model()->FindByPk($data->typeoflaw)->areaoflaw'
这是给这个错误
试图获取非对象的属性"
"Trying to get property of non-object"
可能是由于这个原因,TblAreaoflaw 中不存在某些记录.难道我们不能通过isset检查这一行吗?
Might be due to this reason that the some records doesn't exist in the TblAreaoflaw. Can't we check in this line through isset?
当我放置静态值时,它运行良好,例如
When i put static value, it work well, like
'value'=> 'TblAreaoflaw::model()->FindByPk(5)->areaoflaw',
'value'=> 'TblAreaoflaw::model()->FindByPk(5)->areaoflaw',
有人可以帮忙吗
非常感谢
推荐答案
你得到的错误是因为这个表达式 TblAreaoflaw::model()->FindByPk($data->typeoflaw)
正在返回 null
.这意味着您正在有效地尝试获取 null->areaoflaw
这将不起作用(这就是错误消息 试图获取非对象的属性"澄清).
The error you get is because this expression TblAreaoflaw::model()->FindByPk($data->typeoflaw)
is returning null
. This means that you are effectively trying to get null->areaoflaw
which won't work (this is what the error message "Trying to get property of non-object" clarifies).
我最好的猜测是 $data->typeoflaw
为 TblAreaoflaw 模型返回一个不存在的主键.
My best guess is that $data->typeoflaw
returns a non-existing primary key for the TblAreaoflaw model.
这篇关于Yii 框架 - 从其他模型中获取字段值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!