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
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;
}