我正在尝试在我的ActiveRecord Search()中组合3个表。
我有这张桌子
employees
id
first_name
projects
id
name
project_assignment
id
employee_id
project_id
date_added
date_removed
我使用GridView显示数据库中的所有员工。 现在,我想显示分配员工的项目。所以说到这一点,显然我们需要这三个表来连接。
在我的Employees.php模型中,我有这段代码:
public function getproject_assignment()
{
return $this->hasMany(ProjectAssignment::className(), ['employee_id' => 'id']);
}
在我的ProjectAssignment.php模型中
/**
* @return \yii\db\ActiveQuery
*/
public function getEmployee()
{
return $this->hasOne(Employees::className(), ['id' => 'employee_id']);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getProject()
{
return $this->hasOne(Projects::className(), ['id' => 'project_id']);
}
在我的EmployeeSearch.php
中class EmployeesSearch extends Employees
{
public $project_name;
public function rules()
{
return [
[['id', 'employment_status_id'], 'integer'],
[['project_name','string_id', 'first_name', 'last_name', 'middle_name', 'gender', 'birth_date', 'civil_status', 'phone', 'address', 'zip', 'email', 'position', 'start_date', 'tin', 'philhealth', 'sss', 'hdmf', 'photo_location'], 'safe'],
];
}
public function search($params)
{
$query = Employees::find();
$query->addSelect(['projects.name as project_name','employees.*']);
$query->leftJoin('project_assignment','project_assignment.employee_id = employees.id');
$query->leftJoin('projects','projects.id = project_assignment.project_id');
$query->andWhere([
'project_assignment.date_removed' => NULL
]);
$dataProvider = new ActiveDataProvider([
'query' => $query,
'pagination' => array('pageSize' => 20),
]);
$this->load($params);
if (!$this->validate()) {
// uncomment the following line if you do not want to any records when validation fails
// $query->where('0=1');
return $dataProvider;
}
$query->andFilterWhere([
'id' => $this->id,
]);
$query->andWhere([
'employees.deleted_at' => NULL
// 'projects.deleted_at' => NULL
]);
$query->andFilterWhere(['like', 'string_id', $this->string_id])
->andFilterWhere(['like', 'first_name', $this->first_name])
->andFilterWhere(['like', 'last_name', $this->last_name])
->andFilterWhere(['like', 'middle_name', $this->middle_name])
->andFilterWhere(['like', 'gender', $this->gender])
->andFilterWhere(['like', 'civil_status', $this->civil_status])
->andFilterWhere(['like', 'phone', $this->phone])
->andFilterWhere(['like', 'address', $this->address])
->andFilterWhere(['like', 'zip', $this->zip])
->andFilterWhere(['like', 'email', $this->email])
->andFilterWhere(['like', 'position', $this->position])
->andFilterWhere(['like', 'tin', $this->tin])
->andFilterWhere(['like', 'philhealth', $this->philhealth])
->andFilterWhere(['like', 'sss', $this->sss])
->andFilterWhere(['like', 'hdmf', $this->hdmf])
->andFilterWhere(['like', 'photo_location', $this->photo_location]);
return $dataProvider;
}
在我的index.php(查看文件)
中$gridColumns = [
[
'attribute' => 'Project',
'value' => 'projects', //the value means that this is the value of the column
//the zip here is the get parameter
'filter' => Html::activeDropDownList($searchModel, 'project_name', ArrayHelper::map(\app\models\Projects::find()->asArray()->all(), 'id', 'name'),['class'=>'form-control','prompt' => 'Select Project']),
],
'first_name',
'middle_name',
'last_name',
// 'position',
[
'attribute' => 'position',
'value' => 'position',
'filter' => Html::activeDropDownList($searchModel, 'position', ArrayHelper::map(Employees::find()->groupBy('position')->asArray()->all(), 'position', 'position'),['class'=>'form-control','prompt' => 'Select Position']),
],
['class' => 'yii\grid\ActionColumn',
'template' => '{update} {delete}'],
];
<?php echo
GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => $gridColumns,
'export' => [
'fontAwesome' => true,
]
]);
?>
这里的问题是Project列没有显示任何内容。 这是一个截图:
当我检查Yii Debuger时,已经运行了这个SQL查询:
SELECT `projects`.`name` AS `project_name`, `employees`.* FROM `employees` LEFT JOIN `project_assignment` ON project_assignment.employee_id = employees.id LEFT JOIN `projects` ON projects.id = project_assignment.project_id WHERE (`project_assignment`.`date_removed` IS NULL) AND (`employees`.`deleted_at` IS NULL) LIMIT 20
这个查询是正确的,也是我所期待的。唯一的问题是Project列没有显示为我在上面显示的屏幕截图。
我不确定如何使这项工作。已经抓了几个小时了。
请帮忙。谢谢!
答案 0 :(得分:2)
'value' => 'projects', //the value means that this is the value of the column
实际上这里的value
可以是 string 或 closure 。如果它是字符串,则表示表示要在此列中显示的属性名称的字符串为documentation says。尝试使用闭包。在你的情况下,它将是这样的:
'value' => function ($model, $key, $index, $column){
return $model->project->name;
}
答案 1 :(得分:0)
用于kartik \ grid \ GridView;
data contratosnodato_modi;
format DEF_PRDT $40. DEF_SGMT $40.;
set spain.Tabla_product;
set spain.Tabla_segment;
set spain.contratonodato;
array product {20} a_def_prdt1 b_def_prdt2 c_def_prdt3 d_def_prdt4 e_def_prdt5 f_def_prdt6 g_def_prdt7 h_def_prdt8 i_def_prdt9 j_def_prdt10 k_def_prdt11 l_def_prdt12 m_def_prdt13 n_def_prdt14 o_def_prdt15 p_def_prdt16 q_def_prdt17 r_def_prdt18 s_def_prdt19 t_def_prdt20;
array segment {20} a_def_sgmt1 b_def_sgmt2 c_def_sgmt3 d_def_sgmt4 e_def_sgmt5 f_def_sgmt6 g_def_sgmt7 h_def_sgmt8 i_def_sgmt9 j_def_sgmt10 k_def_sgmt11 l_def_sgmt12 m_def_sgmt13 n_def_sgmt14 o_def_sgmt15 p_def_sgmt16 q_def_sgmt17 r_def_sgmt18 s_def_sgmt19 t_def_sgmt20;
DEF_PRDT = product{posi};
DEF_SGMT = segment{posi};
keep DEF_PRDT DEF_SGMT;
run;