Hello,
I need your help, please.
I have a database with 3 tables. My database model is in attachment!
Now, in the Gridview of "colaborador", i want to create a new column with data from "carreira" table.
The values must be filled this way: "colaborador" has a "categoriaprofissional" that belong to a "carreira".
So the atribute "categoriaprofissional_fk" in the table "colaborador" permits to access to the "categoriaprofissional" which has a foreign key "carreira_fk" that access to the "carreira".
I’m in trouble to achieve the solution for this problem.
This is my index view of "colaborador":
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'numerofuncionario',
'nome',
//now this is the new column that i want to create
[
'label' => 'Carreira',
'attribute' => 'categoriaprofissional_fk',
'value' => categoriaprofissional::get_carreira('categoriaprofissionalFk.carreira_fk'),
'filter'=> categoriaprofissional::get_carreiras(),
],
//this is the "categoriaprofissional" related directly with "colaborador" and works perfectly!
[
'attribute' => 'categoriaprofissional_fk',
'value' => 'categoriaprofissionalFk.nome',
'filter'=> categoriaprofissional::get_nomes(),
],
[
'class' => 'yii\grid\ActionColumn',
'template' => '{view}',
],
],
]); ?>
And in the “categoriaprofissional” model i’ve created some functions to retrived data from “carreira”:
public static function get_nomes(){
$cat = Categoriaprofissional::find()->orderBy(['nome' => SORT_ASC])->all();
$cat = ArrayHelper::map($cat, 'idcategoriaprofissional', 'nome');
return $cat;
}
public static function get_carreira($id){
$car = Carreira::find()->where(['idcarreira' => $id])->one();
return $car;
}
public static function get_carreiras(){
$cars = Carreira::find()->orderBy(['nome' => SORT_ASC])->all();
$cars = ArrayHelper::map($cars, 'idcarreira', 'nome');
return $cars;
}
/**
* @return \yii\db\ActiveQuery
*/
public function getCarreiraFk()
{
return $this->hasOne(Carreira::className(), ['idcarreira' => 'carreira_fk']);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getColaboradors()
{
return $this->hasMany(Colaborador::className(), ['categoriaprofissional_fk' => 'idcategoriaprofissional']);
}
It doesn’t crash but the result wasn’t the expected (or what is my wish )
The filter is populated with the correct values.
The rows are populated with the categoriaprofissional_fk and i wish that the categoriaprofissional_fk worked as a related atribute to obain the categoriaprofissionalFk.carreira_fk (the same way that i obtain the categoriaprofissionalFk.carreira_fk in the last column (categoria profissional) to work as parameter for the categoriaprofissional::get_carreira(‘categoriaprofissionalFk.carreira_fk’) function.
Need your help, please.
Thank you