Yii2 Gridview error

Multi tool use
Multi tool use


Yii2 Gridview error



I want to display column from another table in gridview.



In my controller actionIndex:


public function actionIndex()
{
$user_id = Yii::$app->user->identity->id;
$queryFile = PowerGen::find();
$queryFile ->select(['submitted_by','filename.file_name','filename.submitted_by'])
->joinWith('filename')//Tells Yii to use the complains relation that we define below. By default it is an inner join
->where(['filename.submitted_by' => $this->user_id]);

$dataProvider= new ActiveDataProvider([
'query' => $query,
'pagination' => false,
]);

return $this->render('index', ['dataProvider4'=>$dataProvider]);
}



In my model:


public function fileName()
{
$user_id = Yii::$app->user->identity->id;
return $this->hasMany(Filename::className(), 'submitted_by' => $this->user_id);
}



Error is:


Error
PHP Parse Error – yiibaseErrorException
syntax error, unexpected '=>' (T_DOUBLE_ARROW)



What is the error in the line..



Thank you




3 Answers
3



Use hasMany() like


public function fileName()
{
$user_id = Yii::$app->user->identity->id;
return $this->hasMany(Filename::className(), ['submitted_by' => $this->user_id]);
}





I already used it. but it has error
– Mr Second
Jul 3 at 5:13





Oh, i see you added brackets. let me try
– Mr Second
Jul 3 at 5:23





yes, please refer the given link
– vishuB
Jul 3 at 5:24





for "$this->user_id", how can I tag the logged user?
– Mr Second
Jul 3 at 5:30





Using $this->user_id in relation definition is incorrect.
– rob006
Jul 3 at 18:41


$this->user_id



First i think your function must be like this:


public function fileName()
{
return $this->hasMany(Filename::className(), ['submitted_by' => 'user_id']);
}



And your query like this:


$user_id = Yii::$app->user->identity->id;
$queryFile = PowerGen::find();
$queryFile ->select(['submitted_by','filename.file_name','filename.submitted_by'])
->joinWith('filename')
->where(['filename.submitted_by' => $user_id]);



You are declaring the variable $user_id


$user_id



$user_id = Yii::$app->user->identity->id;



but you are not using it anywhere.





I need to display only the files submitted by the logged user.
– Mr Second
Jul 5 at 8:02



You're defining your relation in a wrong way:


get


getFileName()


hasMany()


Yii::$app->user->identity->id



 


public function getFileName() {
return $this->hasMany(Filename::className(), ['submitted_by' => 'user_id']);
}





I need to display the files submitted by the logged user.
– Mr Second
Jul 5 at 8:02





Then you should add this condition in controller. Model should not be responsible for such magic.
– rob006
Jul 5 at 8:32






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

ZnK0CCRV,UL zaeLFoV fHAl0PH4N0Wv58Uue,nN dQFDfD72OcrKxj9UtNRrNR16 GyCC2CaJxccf
d,mrBOsZ6x2hM,0IZmg UEYjPT66jWvJ,21MHY,q,o uEj2PJYBq7C28qFeiDPT7KVwH5Kwcgl2Zv,5qa

Popular posts from this blog

PHP contact form sending but not receiving emails

Do graphics cards have individual ID by which single devices can be distinguished?

Create weekly swift ios local notifications