2017-03-27 208 views
0

這是我的用戶模型yii2 ActiveData提供商

class User extends \yii\db\ActiveRecord 
{ 

public static function tableName() 
{ 
    return 'user'; 
} 

/** 
* @inheritdoc 
*/ 
public function rules() 
{ 
    return [ 
     [[ 'created_at', 'updated_at', 'branch_id', 'postcode', 'tel_no', 'child_no','company_id'], 'integer'], 
     // [['branch_id', 'edu_level', 'date_joined','address1','country','state','city','postcode','race','position_level','religion','gender','staff_id','username','password','ic_number','tel_no','marital_status','child_no','bumi_status','resident_status','email','company_id'], 'required'], 
     [['get_mail', 'gender', 'marital_status', 'resident_status', 'bumi_status','designation','status'], 'string'], 
     [['date_joined'], 'safe'], 
     [['staff_id', 'password', 'edu_level', 'position_level', 'address2', 'address3', 'address4', 'country', 'state', 'city', 'race', 'religion'], 'string', 'max' => 100], 
     [['username', 'fullname', 'password_hash', 'password_reset_token', 'email', 'auth_key'], 'string', 'max' => 255], 
     [['ic_number'], 'string', 'max' => 14], 
     [['address1'], 'string', 'max' => 1000], 
     [['staff_id'], 'unique'], 
    ]; 
} 

/** 
* @inheritdoc 
*/ 
public function attributeLabels() 
{ 
    return [ 
     'id' => Yii::t('user', 'ID'), 
     'staff_id' => Yii::t('user', 'Staff ID'), 
     'username' => Yii::t('user', 'Username'), 
     'fullname' => Yii::t('user', 'Fullname'), 
     'password' => Yii::t('user', 'Password'), 
     'password_hash' => Yii::t('user', 'Password Hash'), 
     'password_reset_token' => Yii::t('user', 'Password Reset Token'), 
     'email' => Yii::t('user', 'Email'), 
     'ic_number' => Yii::t('user', 'Ic Number'), 
     'auth_key' => Yii::t('user', 'Auth Key'), 
     'status' => Yii::t('user', 'Status'), 
     'created_at' => Yii::t('user', 'Created At'), 
     'updated_at' => Yii::t('user', 'Updated At'), 
     'company_id' => Yii::t('user', 'Company'), 
     'branch_id' => Yii::t('user', 'Branch'), 
     'edu_level' => Yii::t('user', 'Education Level'), 
     'position_level' => Yii::t('user', 'Position Level'), 
     'designation' => Yii::t('user', 'Designation'), 
     'get_mail' => Yii::t('user', 'Get Mail'), 
     'date_joined' => Yii::t('user', 'Date Joined'), 
     'gender' => Yii::t('user', 'Gender'), 
     'address1' => Yii::t('user', 'Address1'), 
     'address2' => Yii::t('user', 'Address2'), 
     'address3' => Yii::t('user', 'Address3'), 
     'address4' => Yii::t('user', 'Address4'), 
     'country' => Yii::t('user', 'Country'), 
     'state' => Yii::t('user', 'State'), 
     'city' => Yii::t('user', 'City'), 
     'postcode' => Yii::t('user', 'Postcode'), 
     'tel_no' => Yii::t('user', 'Tel No'), 
     'marital_status' => Yii::t('user', 'Marital Status'), 
     'child_no' => Yii::t('user', 'Child No'), 
     'race' => Yii::t('user', 'Race'), 
     'religion' => Yii::t('user', 'Religion'), 
     'resident_status' => Yii::t('user', 'Resident Status'), 
     'bumi_status' => Yii::t('user', 'Bumi Status'), 
    ]; 
} 

public static function find() 
{ 
    return new UserQuery(get_called_class()); 
} 

public function getCountries() 
{ 
    return $this->hasOne(Countries::classname(),['id'=>'country']); 
} 

public function getStates() 
{ 
    return $this->hasOne(States::classname(),['id'=>'state']); 
} 

public function getNext_of_kin() 
{ 
    return $this->hasMany(NextOfKin::classname(),['staff_id'=>'staff_id']); 
} 

} 

這是我的看法控制器

public function actionView($staff_id) 
{ 
     $request = Yii::$app->request; 
     $model = $this->findModel($staff_id); 
     $model2 = $model ->next_of_kin; 
     //$sql = "SELECT next_of_kin.name AS Name FROM user left join next_of_kin ON next_of_kin.staff_id = user.staff_id"; 

     $dataProvider = new ActiveDataProvider([ 
     'query' => User::find() ->joinWith(['next_of_kin'])-> where(['next_of_kin.staff_id' => $staff_id]), 
    ]); 

    if($request->isAjax) 
     { 
      Yii::$app->response->format = Response::FORMAT_JSON; 

      return [ 
        'title'=> "User #".$staff_id, 
        'content'=>$this->renderAjax('view', [ 
         'model' => $model, 
         'dataProvider' => $dataProvider, 
        ]), 
        'footer'=> Html::button('Close',['class'=>'btn btn-default pull-left','data-dismiss'=>"modal"]). 
          Html::a('Edit',['update','staff_id'=>$staff_id],['class'=>'btn btn-primary','role'=>'modal-remote']) 
       ];  
     } 
     else 
     { 
      return $this->render('view', [ 
       'model' => $model, 
       'dataProvider' => $dataProvider, 
       "size" => "modal-lg", 
      ]); 
     } 
} 

這是

[ 
       'label' => 'Next of Kin Details', 
       'content' => GridView::widget([ 
         'dataProvider' => $dataProvider, 
         'columns' => [ 
          ['class' => 'yii\grid\SerialColumn'], 
          'username', 
          'fullname', 
          'staff_id', 
          'next_of_kin.name', 
        ], 
       ]), 
      ], 

我想從表名視圖文件next_of_kin基於用戶表中的員工ID。但是,保持節目的時候我嘗試查看它沒有設置

+0

next_of_kin是表名?或者是用戶模型中聲明的關係函數的名稱? – scaisEdge

+0

@scaisEdge兩者的它。我把相同的名稱 – Ron

+0

更新你的問題,並添加relatioal功能。對於用戶模型,請next_of_kin ..並在您使用$數據提供程序 – scaisEdge

回答

0

爲什麼要使用數組,當你想用一個表/關係加入,我的意思是它不應該是錯的,但它的混亂。 您的查詢似乎並沒有錯,找調試器的數據庫模塊,看看有什麼實際的查詢Yii的AR組成,並在數據庫中的原始形式運行它。查詢本身不破,至少從我可以告訴,DB架構,事實上,你可以查詢有沒有回報,你通過$ staff_id和其他一些東西,有可能被打破的方式staff_id。

0

activeDataProvider返回模型的集合

你可以從數據提供程序

檢索模型的陣列
$myModels = $dataProvider->getModels(); 

例如:獲得的第一個元素(用戶模式)next_of_kin模型

$my_next_of_kin = $myModels[0]->next_of_kin; 

迭代通過模型獲得價值;

foreach ($my_next_of_kin as $key => $value) { 
    echo $value->your_next_of_kin_col; 
    } 
0

它只能與一個一對一的關係!你有一對多的。

public function getNext_of_kin() 
{ 
    return $this->hasMany(NextOfKin::classname(),['staff_id'=>'staff_id']); 
} 

'columns' => [ 
     ['class' => 'yii\grid\SerialColumn'], 
     'username', 
     'fullname', 
     'staff_id', 
     'next_of_kin.name', // "next_of_kin" should be one-to-one relation 
],