CakePhp - 如何访问find-> innerJoinWith中的变量?

时间:2016-02-08 10:44:10

标签: php mysql cakephp

我正在尝试查询分配给CakePHP中某个项目的用户。我怎么能基本实现这个目标:

    $projectId = //Project ID query result.

    $users = $this->Tickets->Users
        ->find('list', ['limit' => 200])
        ->innerJoinWith(
            'ProjectsUsers', function($q){
                return $q->where(['ProjectsUsers.project_id' => $projectId]);
            }
        );

此代码在不使用变量时有效(例如,将$ projectId替换为8)但当我尝试使用变量时,我得到:Undefined variable: projectId

如何将变量传递给innerJoinWith?

1 个答案:

答案 0 :(得分:3)

如果您的意思是如何从父作用域继承变量,则可以这样做。

 $users = $this->Tickets->Users
        ->find('list', ['limit' => 200])
        ->innerJoinWith(
            'ProjectsUsers', function($q) use($variableToPass) {
                return $q->where(['ProjectsUsers.project_id' => $variableToPass]);
            }
        );