如何访问 activeRecord 中模型的关系?
How to access to relations of a model in 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']);
我有一个像下面这样的模型:
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']);