小编Rob*_*1on的帖子

对象的Yii2属性

在我的数据库中,我有:带有fileds的区域表

 -id
 -name
Run Code Online (Sandbox Code Playgroud)

和花桌

 -id
 -name
 -region - this is ID from ragion table
Run Code Online (Sandbox Code Playgroud)

在我的Flower模型中我有方法

public function getRegion()
    {
        return $this->hasOne(Region::className(), ['id' => 'region']);
    }
Run Code Online (Sandbox Code Playgroud)

现在当我尝试像这样使用它

$flower->region->name
Run Code Online (Sandbox Code Playgroud)

我收到了错误 Trying to get property of non-object

$flower->region
Run Code Online (Sandbox Code Playgroud)

返回区域ID.如何通过使用获取区域名称

$flower->region->name
Run Code Online (Sandbox Code Playgroud)

更新:当我使用gii生成Flower模型时,我得到了这个方法:

public function getRegion0()
    {
        return $this->hasOne(Region::className(), ['id' => 'region']);
    }
Run Code Online (Sandbox Code Playgroud)

我不懂.为什么我不能使用简单的getRegion()

php activerecord yii2

0
推荐指数
1
解决办法
74
查看次数

标签 统计

activerecord ×1

php ×1

yii2 ×1