Rob*_*1on 0 php activerecord 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()