php - 如何访问 activeRecord 中模型的关系?
问题描述
我有一个像下面这样的模型:
class Book extends ActiveRecord
{
{ public function getDomain()
{
return $this->hasOne(Domain::className(), ['ID' => 'domainID']);
}
public function getOwnerPerson()
{
return $this->$this->hasOne(Person::className(), ['ID' => 'ownerPersonID']);
}
public function getCreatorUser()
{
return $this->$this->hasOne(User::className(), ['ID' => 'creatorUserID']);
}
public function getUpdaterUser()
{
return $this->$this->hasOne(User::className(), ['ID' => 'updaterUserID']);
}
}
我通过以下方式从 Book 模型创建了一个对象:
$model=Book::find()->all();
当我使用 时$model->domain
,一切正常,但是当我使用时$model->ownerPerson
,它会引发错误:
Object of class backend\models\Book could not be converted to string
问题是什么?
解决方案
删除第二个 $this。
return $this->$this->hasOne(Person::className(), ['ID' => 'ownerPersonID']);
to
return $this->hasOne(Person::className(), ['ID' => 'ownerPersonID']);
推荐阅读
- excel - 无法使用 vba 在散点图上绘制值
- ios - 如何强制 iOS 12 的主屏幕 Web 应用程序自动更新?
- javascript - 当焦点在输入字段中时,为什么日期选择器在 React 中闪烁?
- vba - 更换 PowerPoint 演示文稿中的字体时遇到问题
- c - 为什么函数指针不命名它们的参数?
- widget - 例如,如何在 odoo12 中创建一个下拉列表并用 1 到 5 的值填充它
- python - 只允许两个人使用不和谐的机器人命令
- python - 在 mac os 上安装 psycopg2 失败
- python - 如何管理两个线程,pynput鼠标监听器和while循环?
- json - 在弹性搜索中存储布尔值:优化?