2

I am trying to query for users that are assigned to a certain project in CakePHP. How could I basically achieve this:

    $projectId = //Project ID query result.

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

This code works when not using variables (eg. replacing $projectId with 8) but when I try to use variables I get: Undefined variable: projectId

How can I pass variables into innerJoinWith?

RusinaRange
  • 341
  • 1
  • 4
  • 18
  • 1
    Do you actually have this line in your code? $projectId = //Project ID query result. That is not how to initialize a variable. – ArrowHead Feb 08 '16 at 10:47
  • Ofc not, it's just an abstraction for that query since it works and is not relevant to the questin. – RusinaRange Feb 08 '16 at 10:51

1 Answers1

3

If you mean how to inherit a variable from the parent scope, you'd do it like this.

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