Getting error using relation of yii2

Hi guys, using hasOne()-relation,created by Gii will 'cause following error:





PHP Warning – yii\base\ErrorException

htmlspecialchars() expects parameter 1 to be string, object given

.

.

.




public function actionIndex() {

        $searchModel = new BranchesSearch();

        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index', [

   				'searchModel' => $searchModel,

   				'dataProvider' => $dataProvider//this is faulty according to debugger




If I read out ForeignKeyValue as it is, I wont get error. If I use relation, Iwill get error:Here is relation:





    public function getCompaniesCompany()

    {

        return $this->hasOne(Companies::className(), ['company_id' => 'companies_company_id']);

   }



Here is GridView,where error will be raised:





<?=

   GridView::widget([

        'dataProvider' => $dataProvider,

   	'filterModel' => $searchModel,

 		'companiesCompany', // this is raising error

   	'companies_company_id' //this won't raising error



Any ideas,where is bug?

Everything has been created using Gii…!

Try the following:




<?=

   GridView::widget([

        'dataProvider' => $dataProvider,

   	'filterModel' => $searchModel,

        'columns' => [

 		'companiesCompany.company_id',

   	        'companies_company_id',



In the definition array of ‘columns’, you can use a dot syntax for related model, ‘relationName.attributeName’ for example.

That’s not my intention. I want value of corresponding parentable,not value of ForeignKey. Creating a relation should bring me this value,but I get error as described.

For better understanding what I intend, I will show U code,which will solve my problem without using relations hasOne()/hasMany() of framework:







                'attribute' => 'companies_company_id',

                'contentOptions' => [

                    'style' => ['width' => '300px;']],

                'label' => Yii::t('app', 'Firma(FK)'),

                'format' => 'html', // sorgt dafür,dass das HTML im return gerendert wird

                'value' => function($model) {

                    $firma = frontend\models\Departments::getFirma($model);

                    return $firma;

                }

            ],



method im model:





    public static function getFirma($model) {




   	return Companies::find()

                        ->leftJoin('departments', 'companies.company_id=departments.companies_company_id')

               		->where(['departments.companies_company_id' => $model::findOne([$model->department_id])->companies_company_id])->one()->company_name;

    }



I’m a fool

Here is code which will substantiate my intention using relations of framework





            [

                'attribute' => 'companies_company_id',

                'label' => Yii::t('app', 'Firma(FK)'),

               'value' => function($model) {

                   if ($model->companies_company_id) {

                        return $model->companiesCompany->company_name;

                    } else {

                       return NULL;

                    }

               },

            ],



I hope the following should also work:




    [

        'attribute' => 'companies_company_id',

        'label' => Yii::t('app', 'Firma(FK)'),

        'value' => 'companiesCompany.company_name',

    ],



http://www.yiiframework.com/doc-2.0/yii-grid-datacolumn.html#$value-detail

And try the following just for a test:




//    [

//        'attribute' => 'companies_company_id',

//        'label' => Yii::t('app', 'Firma(FK)'),

//        'value' => 'companiesCompany.company_name',

//    ],

    'companiesCompany.company_name',



Worked just fine for me, thank you so much @softark for the codes :)

Worked just fine for me, thank you so much @softark for the codes I am using it on some of my websites.