sws229
(Jaspermcmillan)
August 16, 2014, 3:31pm
1
In my view action, I have a Query Builder function with parameter binding; like so:
public function actionView($slug)
{
$books = Post::find()
->joinWith(['tags' => function($query) {
$query->where('tags.slug = :slug');
$query->params(':slug' => $slug);
}])
->where('status = :status', ['status' => 1]);
...
}
However, I can’t use the $slug variable as in the parameter binding. Error returned was “Undefined variable: slug ”. How can I use that variable in Query Builder function?
tuga
(Tuga74)
August 16, 2014, 5:35pm
2
not sure but I would start with this, i think it should be an array
$query->params([':slug' => $slug]);
evstevemd
(Stefano Mtangoo)
August 16, 2014, 9:13pm
3
what about this
public function actionView($slug)
{
$books = Post::find()
->joinWith(['tags' => function($query) {
$query->where(['tags.slug' => $slug]);
}])
->andWhere(['status' =>1]);
...
}
alirz23
(Alirz23)
August 16, 2014, 10:08pm
4
$books = Post::find()
->joinWith(['tags' => function($query) use($slug) {
$query->where('tags.slug = :slug');
$query->params([':slug' => $slug]);
}])
->where('status = :status', ['status' => 1]);
sws229
(Jaspermcmillan)
August 19, 2014, 2:23pm
5
tuga:
not sure but I would start with this, i think it should be an array
$query->params([':slug' => $slug]);
Thanks for pointing that out.
Does not work, the ‘$slug’ variable must be passed to the query function. Reply below solves it.
This solves it. Thank you very much!