我有一个名为Account.php的模型:
class Account extends \yii\db\ActiveRecord
该表格包含以下字段:
public function rules()
{
return [
[['company_id', 'account_type_id', 'default_rate_id'], 'integer'],
[['prepaid_only', 'email_send', 'active', 'archived'], 'boolean'],
[['address_mailing', 'email_addresses'], 'string'],
[['archived_date'], 'safe'],
[['number'], 'string', 'max' => 45],
[['name'], 'string', 'max' => 90]
];
}
要根据外键(FK)检索数据,我使用以下功能:
public function getAccountType()
{
return $this->hasOne(AccountType::className(), ['id' => 'account_type_id']);
}
public function getAccountTypeName()
{
return $this->accountType->name;
}
public function getRate()
{
return $this->hasOne(Rate::className(), ['id' => 'default_rate_id']);
}
public function getRateName()
{
return $this->rate->name;
}
问题:该代码适用于getAccountTypeName,但是对于getRateName,我收到以下错误:
尝试获取非对象的属性
为什么我只在其中一个功能上出现错误?