dongwuxie5112 2013-02-11 17:40
浏览 8

如何在ORM中制作过滤器?

For example my Model has method get_objects, I am searching them by following source:

class Model_Object extends ORM {

public function get_objects($filters = array())
{

        if (!empty($filters['Role']))
            $role = $filters['Role'];
          else
        $role = NULL;


        $objects = ORM::factory('object')
                         ->where('RoleId','=',$role)
                         ->find_all();

       return $objects;
}

So that code will work only when the filter exists, when there's no values in filter I'll not have any records instead of all (I want all records then), how to make it better?

  • 写回答

1条回答 默认 最新

  • doufei3152 2013-02-12 00:12
    关注

    It sounds like you want to make the ORM where call dependent on the relevant filter key being present. The following code should do what you're looking for, it works for me, does it do what you want?

    It would be neater if it didn't allow for the key-name/field-name distinction (Role vs. RoleId).

    application/classes/Model/Object:

    class Model_Object extends ORM {
    
        // Map from filter key names to model field names
        protected static $_filter_map = array(
            'Role' => 'RoleId',
            // ...
        );
    
        public static function get_objects($filters = array())
        {
    
            $objects = ORM::factory('object');
    
            foreach($filters as $key => $value)
            {
                if ($field = Arr::get(self::$_filter_map, $key))
                {
                    $operator = (is_array($value)) ? 'IN' : '=';
                    $objects->where($field, $operator, $value);
                } else {
                    throw new Kohana_Exception(
                        'Unknown filter key :key',
                        array(':key' => $key)
                    );
                }
            }
    
            return $objects->find_all();
        }
    }
    

    Some examples, first all objects:

    Model_Object::get_objects( array() );
    

    All objects with RoleId equal to 2:

    Model_Object::get_objects( array('Role' => 2) );
    

    All objects with role_id in (2,3,5) AND user_id = 1:

    Model_Object::get_objects( array(
        'Role' => array(2,3,5),
        'User' => 1
    ) );
    

    Note that this last one requires you to have:

    protected static $_filter_map = array(
        'Role' => 'role_id',
        'User' => 'user_id', // where `user_id` is a db field name
        // ...
    );
    

    OR to change/simplify the get_objects function to

    $objects = ORM::factory('object');
    
    foreach($filters as $key => $value)
    {
        $field = Arr::get(self::$_filter_map, $key, $key);
        $operator = is_array($value) ? 'IN' : '=';
        $objects->where($field, $operator, $value);
    }
    
    return $objects->find_all();
    
    评论

报告相同问题?

悬赏问题

  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果