简体   繁体   English

如何在cakephp 3.x中使用“不介于”

[英]How to use “not between” in cakephp 3.x

I want to query not between in cakephp 3.x 我想查询不之间在cakephp 3.x

I can use between like this. 我可以这样使用。 But I can't use not between. 但是我不能在两者之间不使用。

array(function ($exp) use ($field_value, $txtRule1, $txtRule2) {
    return $exp->between($field_value, $txtRule1, $txtRule2);
});

Any one have idea about how to use not between in cakephp 3.x 任何人都有关于如何在cakephp 3.x之间不使用的想法

Thanks and Best Regards. 谢谢和最好的问候。

Just wrap it in QueryExpression::not() : 只需将其包装在QueryExpression::not()

use \Cake\Database\Expression\BetweenExpression;
use \Cake\Database\Expression\QueryExpression;

// ...

function (QueryExpression $exp) use ($field_value, $txtRule1, $txtRule2) {
    return
        $exp->not(
            new BetweenExpression(
                $field_value,
                $txtRule1,
                $txtRule2,
                $exp->typeMap()->type($field_value)
            )
        );
}

The new expression instance is required as you'd otherwise nest the expression in itself, which would cause an infinite loop at some point. 需要新的表达式实例,否则将表达式自身嵌套,这将在某个时候导致无限循环。 You could also use cloning in case you are sure that you are working with an empty expression instance 如果您确定要使用空的表达式实例,也可以使用克隆

function (QueryExpression $exp) use ($field_value, $txtRule1, $txtRule2) {
    $between = clone $exp;
    return
        $exp->not(
            $between->between($field_value, $txtRule1, $txtRule2)
        );
}

or pass the query instance to the callable and create a new query expression instance 或将查询实例传递给可调用对象并创建新的查询表达式实例

function (QueryExpression $exp) use ($field_value, $txtRule1, $txtRule2, $query) {
    return
        $exp->not(
            $query->newExpr()->between($field_value, $txtRule1, $txtRule2)
        );
}

See also 也可以看看

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

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