排除来自Lithium find的结果

mic*_*ish 4 php lithium

我想排除调用Lithium模型find()方法的结果.我需要为包含MongoDB和MySQL数据源的模型执行此操作,但在SQL中我的意思是类似的WHERE myfield NOT IN (1,2,3).

我想能够像下面not那样在conditions数组中传递一个子句,但这似乎不可能.

Item::all(array('conditions' => array('not' => array('myfield' => array(1,2,3))));
Run Code Online (Sandbox Code Playgroud)

所以我的问题是,这是否有可能在锂中以我忽略的方式?如果没有,那么为我的模型实施它的最简单方法是什么?

Nat*_*ele 12

为了澄清一下,Lithium的MongoDB适配器支持大多数SQL比较运算符,因此对于Mongo MySQL,您只需按如下方式编写查询:

Item::all(array('conditions' => array(
    'myfield' => array('!=' => array(1,2,3))                                                                                                                                                                                 
)));
Run Code Online (Sandbox Code Playgroud)

它应该给你你期望的结果.对于MySQL,查询应该类似于:

SELECT * FROM items WHERE myfield NOT IN (1, 2, 3);
Run Code Online (Sandbox Code Playgroud)

在Mongo:

db.items.find({ myfield: { $nin: [1, 2, 3] }})
Run Code Online (Sandbox Code Playgroud)


Ner*_*too 6

只需过滤MongoDB即可轻松实现:

Item::all(array('conditions' =>                                                                                                                                                                                
    array('myfield' => array(                                                                                                                                                                                  
        '$nin' => array(1,2,3)                                                                                                                                                                                 
    ))                                                                                                                                                                                                         
));                                                                                                                                                                                                            
Run Code Online (Sandbox Code Playgroud)

如果这是你做了很多事情,你甚至可以为它创建一个自定义查找程序:

class MyModel extends \lithium\data\Model {                                                                                                                                                                    
    public static function __init()                                                                                                                                                                            
    {                                                                                                                                                                                                          
        parent::__init();                                                                                                                                                                                      

        static::finder('notin', function($self, $params, $chain) {                                                                                                                                             
            // Take all array keys that are not option keys
            $array = array_diff_key($params['options'],
                array_fill_keys(array('conditions', 'fields','order','limit','page'),0));
            // Clean up options leaving only what li3 expects
            $params['options'] = array_diff_key($params['options'], $array);
            $params['options']['conditions'] = array(
                'myfield' => array(
                    '$nin' => $array
                )
            );

            return $chain->next($self, $params, $chain);                                                                                                                                                       
        });                                                                                                                                                                                                    
    }                                                                                                                                                                                                          
}                                                                                                                                                                                                              
Run Code Online (Sandbox Code Playgroud)

并称之为:

MyModel::notin(array(1,2,3));                                                                                                                                                                                  
Run Code Online (Sandbox Code Playgroud)

以同样的方式,您可以为MySQL源创建自定义查找程序.

正如您可能会看到的那样array('fields'=>$array),如果您传递的内容会产生一些问题,因为它会覆盖该选项.会发生什么::notin()(一般的查找程序)对(数组,空)签名有不同的行为.如果发生这种情况,它认为第一个数组是选项而finder没有参数.使用notin($array,array())中断先前的查找器,因为第一个参数在$params['notin']传递真正的第二个参数(选项)时结束.

如果您在这里动态混合数据源,我将创建一个不继承\ lithium\data\Model的自定义模型,并将其委托
给不同的模型,并根据最终模型数据源创建条件.

class MyFacadeModel {                                                                                                                                                                                          
    public static function byNotIn($conditions, $source) {                                                                                                                                                     
        return ($source == "mongodb")                                                                                                                                                                          
            ? $source::find( $rewrittenConditions)                                                                                                                                                             
            : $source::find( $rewrittenConditionsForMysql );                                                                                                                                                   
    }                                                                                                                                                                                                          
}
Run Code Online (Sandbox Code Playgroud)

(代码可能稍微不正确,因为它主要取自我的头脑)