这是我的用户模型
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',
],
]),
],
我试图根据User表中的staff id从表next_of_kin获取名称。但是当我试图查看它时,它会显示未设置
答案 0 :(得分:0)
为什么在想要使用单个表/关系加入时使用数组,我的意思是它不应该是错误的,但它令人困惑。 您的查询似乎没有错,请查找调试器的数据库模块,以查看Yii AR组成的实际查询,并以原始格式在DB中运行它。查询本身并没有被破坏,至少从我所知道的,数据库架构,你可以查询没有任何东西返回的staff_id,你传递$ staff_id的方式以及其他一些东西可能被打破这一事实。
答案 1 :(得分:0)
activeDataProvider返回模型集合
您可以从dataProvider
中检索模型数组 $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;
}
答案 2 :(得分: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
],