简体   繁体   English

CakePHP包含条件不能使用变量吗?

[英]CakePHP contain condition cannot use variable?

I guess there is an easy solution for this issue, but i cant seem to find it out. 我想这个问题有一个简单的解决方案,但我似乎找不到。 I get back an error saying $p_name equals NULL on the contain Comments query. 我返回一个错误,指出包含注释查询中的$ p_name等于NULL。 How is this possible since it was clearly set before. 既然以前已经明确设置了这怎么可能。

$p_name = "Berta";

$query = $articles->find()->contain([
    'Comments' => function ($q) {
       return $q
            ->select(['name'])
            ->where(['Comments.name' => $p_name]);
    }
]);

The variable is not available inside the scope of function q, you need to pass with the USE construct like: 该变量在函数q的范围内不可用,您需要传递USE构造,例如:

$p_name = "Berta";

$query = $articles->find()->contain([
    'Comments' => function ($q) use ($p_name) {
       return $q
            ->select(['name'])
            ->where(['Comments.name' => $p_name]);
    }
]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM