2015-02-06 2 views
0

user 테이블과 User 모델이 있습니다. 고급 응용 프로그램에 있습니다. User hasOne 관계는 UserType 모델이며 표의 이름은 user_type이며 테이블의 이름은 status입니다. StatusUserType의 두 모델은 User 모델과 많은 관계가 있습니다.Yii2라는 테이블과 user_type 관계 테이블에 사용자 테이블의 사용자 모델

짧은 문제 :

Invalid Parameter – yii\base\InvalidParamException

Relation names are case sensitive. common\models\User has a relation named "userType" instead of "UserType".

나는 또한 제공 UserTypeName로 교체 시도 : DetailView 위젯을 사용하는 관점에서, 나는 userTypeName 다음과 같은 오류를 제공하여이 문제

<?= 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', 
     ], 
    ]) ?> 

발견 같은 오류

내가 laz를 사용하면 만 작동합니다. 예 검색어 'userType.user_type_name'!

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 attributeLabels() 
    { 
     return [ 
     /* Your other attribute labels */ 
     'roleName' => Yii::t('app', 'Role'), 
     'statusName' => Yii::t('app', 'Status'), 
     /* ... */ 
     'userTypeName' => Yii::t('app', 'User Type'), 
     'userTypeId' => Yii::t('app', 'User Type'), 
     'userIdLink' => Yii::t('app', 'ID'), 
     ]; 
    } 

:

그러나, User 모델에서 이미 다음과 같은 attributeLabels() 방법 모두 statusNameuserTypeName의 속성을 정의 또한 다음과 같이 및 userTypeName에 대한 getter를 정의했습니다.

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 -'; 
    } 

userTypeName이 작동하지 않는 동안 pron = blem의 루트가 발견되어 statusName이 작동하지 않습니다. 테이블이나 모델 또는 메서드에 대해 잘못된 명명 규칙이 있습니까? 테이블 이름이 다른 관련 테이블의 이름으로 시작합니까? 즉 useruser_type?

당신은 오타 getUserTypeName 단순히 올바른 존재한다

답변

2

:

public function getUserTypeName() 
{ 
    return $this->userType ? $this->userType->user_type_name : '- no user type -'; 
} 
관련 문제