relational database - Yii2 Framework: GridView, create new column with data from another tablet with indirect relationship -
i need help, please. i'm using yii 2 framework , generate models, controller , views gii!
i have database 3 tables. database model here:
now, in gridview of "colaborador", want create new column data "carreira" table. values must filled way: "colaborador" has "categoriaprofissional" belong "carreira". atribute "categoriaprofissional_fk" in table "colaborador" permits access "categoriaprofissional" has foreign key "carreira_fk" access "carreira".
i'm in trouble achieve solution problem.
this index view of "colaborador":
<?= gridview::widget([ 'dataprovider' => $dataprovider, 'filtermodel' => $searchmodel, 'columns' => [ ['class' => 'yii\grid\serialcolumn'], 'numerofuncionario', 'nome', //now new column want create [ 'label' => 'carreira', 'attribute' => 'categoriaprofissional_fk', 'value' => categoriaprofissional::get_carreira('categoriaprofissionalfk.carreira_fk'), 'filter'=> categoriaprofissional::get_carreiras(), ], //this "categoriaprofissional" related directly "colaborador" , works perfectly! [ 'attribute' => 'categoriaprofissional_fk', 'value' => 'categoriaprofissionalfk.nome', 'filter'=> categoriaprofissional::get_nomes(), ], [ 'class' => 'yii\grid\actioncolumn', 'template' => '{view}', ], ], ]); ?>
and in "categoriaprofissional" model i've created functions retrived data "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 result wasn't expected (or wish :) )
the filter populated correct values. rows populated categoriaprofissional_fk , wish categoriaprofissional_fk worked related atribute obain categoriaprofissionalfk.carreira_fk (the same way obtain categoriaprofissionalfk.carreira_fk in last column (categoria profissional) work parameter categoriaprofissional::get_carreira('categoriaprofissionalfk.carreira_fk') function.
need help, please. thank
Comments
Post a Comment