cakephp3给定义变量的未定义错误

时间:2015-10-09 06:54:03

标签: php mysql cakephp-3.0

我的代码是 -

$user_id = 1;
$users = $this->UserModel->getUser($user_id);

$get_result = $this->Actionmodel->find('all', [
    'conditions' => ['id IN' => $ids],
    'contain' => [
        'Rank' => function ($q) {
            return $q
                ->select(['id', 'user_id', 'description'])
                ->where([
                    'status' => 1,
                    'user_id NOT IN' => $users
                ]);
        }
    ]
]);

给出错误未定义的用户。而它的打印user_id数组。

1 个答案:

答案 0 :(得分:1)

你在封闭中使用$users而没有在use中指定它...就像这样:

$get_result = $this->Actionmodel->find('all',[
                                    'conditions'=>['id IN' => $ids],
                                    'contain'=>['Rank' => function ($q) use ($users) {
                                    return $q
                                    ->select(['id','user_id','description'])->where(['status' => 1,'user_id NOT IN' => $users]);}]]);