RusinaRange February 2016

CakePhp - how to access variables inside find->innerJoinWith?

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?

Answers


JazzCat February 2016

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]);
            }
        );

Post Status

Asked in February 2016
Viewed 2,905 times
Voted 12
Answered 1 times

Search




Leave an answer