這是我的用戶模型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。但是,保持節目的時候我嘗試查看它沒有設置
next_of_kin是表名?或者是用戶模型中聲明的關係函數的名稱? – scaisEdge
@scaisEdge兩者的它。我把相同的名稱 – Ron
更新你的問題,並添加relatioal功能。對於用戶模型,請next_of_kin ..並在您使用$數據提供程序 – scaisEdge