选择语言 :

 Module_OOP_ORM_Parse::get_orm_data_by_config

根据字段ORM配置获取ORM数据

mixed Module_OOP_ORM_Parse::get_orm_data_by_config( OOP_ORM_Data $data_obj , array $orm_config , $index )
throws
Exception

参数列表

参数 类型 描述 默认值
$data_obj OOP_ORM_Data $data_obj
$orm_config array $orm_config
$index unknown
返回值
  • mixed
File: ./modules/oop/orm/parse.class.php
public static function get_orm_data_by_config(OOP_ORM_Data $data_obj, $orm_config, $index)
{
    if ( isset( $orm_config['cache'] ) )
    {
        # 设置了缓存
        $c_config = $orm_config;
        foreach ( $c_config['mapping'] as & $item )
        {
            $item = $data_obj->$item;
        }
        $data = OOP_ORM_Parse::get_orm_cache_data($data_obj , $index , $c_config);

        $orm_name = 'ORM_' . $orm_config['name'] . '_Data';
        if ( $data && is_object($data) && $data instanceof $orm_name )
        {
            return $data;
        }

        $can_try_get_gourps_data = false;
    }
    else
    {
        # 是否可以批量取对象集里的数据
        $can_try_get_gourps_data = true;
    }

    # 映射ORM对象
    $obj_name = 'ORM_' . $orm_config['name'] . '_Finder';
    if ( ! class_exists($obj_name, true) )
    {
        throw new Exception('指定的ORM:' . $orm_config['name'] . '不存在!');
    }
    $obj = new $obj_name();

    # 支持OFFSET
    if ( isset($orm_config['offset']) )
    {
        $obj->offset($orm_config['offset']);
        if ( $orm_config['offset'] > 0 )
        {
            $can_try_get_gourps_data = false; //有OFFSET的话,不能同时取多条数据
        }
    }

    # 支持GROUP BY
    if ( isset($orm_config['group_by']) )
    {
        foreach ($orm_config['group_by'] as $item)
        {
            $obj->group_by($item);
        }
        $can_try_get_gourps_data = false;
    }

    # 支持ORDER BY
    if ( isset($orm_config['order_by']) )
    {
        foreach ($orm_config['order_by'] as $key=>$item)
        {
            $obj->order_by($key , $item);
        }
        $can_try_get_gourps_data = false;
    }

    # 支持IN
    if ( isset($orm_config['in']) )
    {
        foreach ($orm_config['in'] as $key=>$item)
        {
            $obj->in($key , $item);
        }
        $can_try_get_gourps_data = false;
    }

    # 获取ORM单条数据
    if ( $can_try_get_gourps_data && $orm_config['return'] == OOP_ORM::PARAM_RETURN_SINGLE )
    {
        # 对于单条数据,可以尝试批量获取,以优化SQL性能
        $data = OOP_ORM_Parse::_get_orm_group_data($obj, $data_obj, $orm_config , $index);
        if ( true!==$data )
        {
            # 如果!==true,则直接返回
            return $data;
        }
    }

    $orm_where = array();
    # 处理mapping
    if ( $orm_config['mapping'] )
    {
        foreach ( $orm_config['mapping'] as $k => $v )
        {
            $orm_where[$k] = $data_obj->$v;
        }
    }
    if ( isset($orm_config['where']) )
    {
        $orm_where = $orm_config['where'] + $orm_where;
    }

    if ( $orm_where )
    {
        # 设置autowhere
        $obj->set_auto_where($orm_where);
    }

    if ( $orm_config['limit'] )
    {
        $obj->limit($orm_config['limit']);
    }

    if ( $orm_config['return'] == OOP_ORM::PARAM_RETURN_SINGLE )
    {
        # 获取单条数据
        $data = $obj->find()->current();
    }
    elseif ( $orm_config['return'] == OOP_ORM::PARAM_RETURN_GROUP )
    {
        # 获取一组数据
        $data = $data = $obj->find();
    }
    else
    {
        # 当前ORM对象
        $data = $obj;
    }

    if ( isset($orm_config['cache']) )
    {
        # 设置缓存
        OOP_ORM_Parse::set_orm_cache_data($data_obj , $index , $c_config , $data);
    }

    return $data;
}