在cakephp3中的matching或innerJoinWith中传递动态变量 [英] Pass dynamic variable in matching or innerJoinWith in cakephp3
本文介绍了在cakephp3中的matching或innerJoinWith中传递动态变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在将静态值传递给以下查询,因此效果很好.如果我将其动态更改,则不会更改.
I am passing static value to my following query, then it works well. If I change it dynamic, then it doesn't.
//works well
$users = $this->Users->find('all')
->where(['Users.role' => $role])
->innerJoinWith('UserDetail',function($query){
return $query->where(['UserDetail.state' => "chandigarh"]);
})->toArray();
// don't work
$state="chandigarh";
$users = $this->Users->find('all')
->where(['Users.role' => $role])
->innerJoinWith('UserDetail',function($query){
return $query->where(['UserDetail.state' => "$state"]);
})->toArray();
如何将动态的$ state变量传递给它?
How can I pass my $state variable dynamic to this?
推荐答案
这是PHP的工作方式,它实际上不是CakePHP相关的问题
This is how PHP works, it's not really a CakePHP related problem
该变量在匿名函数$ query的范围内不可用请在此处查看示例3: http://php.net/manual/zh/functions.anonymous.php
That variable is not available inside the scope of the anonymous function $query see example 3 here: http://php.net/manual/en/functions.anonymous.php
您应该使用USE结构从父作用域继承变量,如下所示:
You should inherit the variable from the parent scope by using the USE contruct like this:
$state="chandigarh";
$users = $this->Users->find('all')
->where(['Users.role' => $role])
->innerJoinWith('UserDetail',function($query) use ($state) {
return $query->where(['UserDetail.state' => $state]);
})->toArray();
这篇关于在cakephp3中的matching或innerJoinWith中传递动态变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文