Yii2和名为user_类型的表与用户表的用户模型的关系问题
我有一个Yii2和名为user_类型的表与用户表的用户模型的关系问题,yii2,php,mysql,model,relationship,Yii2,Php,Mysql,Model,Relationship,我有一个user表和一个user高级应用程序模型。User与其表名为User\u type的UserType模型有一个关系,也与其表名为Status的Status模型有一个关系。状态和用户类型这两个模型与用户模型有很多关系 简而言之,问题是:在使用DetailViewwidget的视图中,我发现了这个问题 <?= DetailView::widget([ 'model' => $model, 'attributes' => [
user
表和一个user
高级应用程序模型。User
与其表名为User\u type
的UserType
模型有一个关系,也与其表名为Status
的Status
模型有一个关系。状态
和用户类型
这两个模型与用户
模型有很多关系
简而言之,问题是:在使用DetailView
widget的视图中,我发现了这个问题
<?= DetailView::widget([
'model' => $model,
'attributes' => [
['attribute' => 'profileLink', 'format' => 'raw'],
'id',
'email:email',
'statusName', //This works Fine
'userTypeName', // This DOES NOT Work
'roleName',
'created_at',
'updated_at',
],
]) ?>
此外,我对关系的定义如下:
public function getStatus()
{
return $this->hasOne(Status::className(), ['id' => 'status_id']);
}
public function getUserType()
{
return $this->hasOne(UserType::className(), ['id' => 'user_type_id']);
}
public function getStatusName()
{
return $this->status ? $this->status->status_name : '- no status -';
}
public function getUserTypeName()
{
return $this->userType ? $this->UserType->user_type_name : '- no user type -';
}
此外,我还为statusName
和userTypeName
定义了一个getter,如下所示:
public function getStatus()
{
return $this->hasOne(Status::className(), ['id' => 'status_id']);
}
public function getUserType()
{
return $this->hasOne(UserType::className(), ['id' => 'user_type_id']);
}
public function getStatusName()
{
return $this->status ? $this->status->status_name : '- no status -';
}
public function getUserTypeName()
{
return $this->userType ? $this->UserType->user_type_name : '- no user type -';
}
我无法发现pron=blem的根源,它使
statusName
工作,而userTypeName
不工作。表、模型甚至方法是否存在任何错误的命名约定?!表名是否以另一个相关表的名称开头?i、 euser
和user\u type
?您只需更正getUserTypeName
,有一个输入错误:
public function getUserTypeName()
{
return $this->userType ? $this->userType->user_type_name : '- no user type -';
}