Пример #1
0
 /**
  * 根据组ID获取指定的offset和数量的数据
  *
  * 返回 false 表示不存在对应的组
  *
  * @param $group_id
  * @param int $offset 起始位置
  * @param int $limit 返回数量,0表示返回全部
  * @return array 返回数组
  */
 public static function get_data_by_group_id($group_id, $offset = 0, $limit = 0)
 {
     if (!isset(OOP_ORM_Result::$GROUP_DATA[$group_id]) && !isset(OOP_ORM_Result::$GROUP_RESOURCE[$group_id])) {
         return array();
     }
     $max = OOP_ORM_Result::get_count_by_group_id($group_id);
     if ($limit > 0) {
         $max = min($max, $offset + $limit);
     }
     $rs = array();
     for ($i = $offset; $i < $max; $i++) {
         $rs[$i] = OOP_ORM_Result::get_offset_data_by_group_id($group_id, $i);
     }
     return $rs;
 }
Пример #2
0
 protected function __orm_callback_set_key_batch_orm_group(OOP_ORM_Result $result)
 {
     $this->_cached_key_batch_group_ids[] = $group_id = $result->id();
     # 保存起来,待对象销毁时分组才销毁
     OOP_ORM_Data::$KEY_BATCH_GROUPS[$group_id] = $result;
 }
Пример #3
0
 /**
  * 用于给ORM回调初始化数据
  */
 protected function __orm_callback_ini_result_(OOP_ORM_Result $result)
 {
     $this->_orm_result_uniqid = $result->get_uniqid();
 }
Пример #4
0
 /**
  * 批量获取数据
  *
  * @param OOP_ORM $finder
  * @param OOP_ORM_Data $obj
  * @return null|OOP_ORM_Data
  */
 protected function get_data_batch_type(OOP_ORM $finder, OOP_ORM_Data $obj, $type)
 {
     $config = $this->config();
     $mapping = $config['mapping'];
     $where = $config['where'];
     $bind = isset($config['bind']) ? $config['bind'] : null;
     $bind_field = $this->field_name;
     $group = array();
     if ($group_ids = $obj->__orm_callback('get_group_ids')) {
         $obj_class = get_class($obj);
         $next = true;
         // 用来判断使得要跳出while循环
         $offset = 0;
         // 起始位置
         $limit = 100;
         // 单组批量获取数,这样可以避免某些组数量特别多时导致异常问题
         while ($next) {
             $found_count = 0;
             foreach ($group_ids as $group_id) {
                 $tmp_group_data = OOP_ORM_Result::get_data_by_group_id($group_id, $offset, $limit);
                 $found_count += count($tmp_group_data);
                 foreach ($tmp_group_data as $item) {
                     if (get_class($item) !== $obj_class) {
                         # 不是相同类型的对象
                         continue;
                     }
                     if ($item === $obj) {
                         # 同一个对象
                         $next = false;
                     }
                     if ($item->__orm_callback('is_compiled', $this->key)) {
                         # 已经构造过的数据忽略
                         continue;
                     }
                     if ($found_count > $limit) {
                         $next = false;
                     }
                     $group[] = $item;
                 }
             }
             if (!$found_count) {
                 # 没有可用的返回数
                 break;
             }
             $offset += $limit;
         }
     }
     if (!$group) {
         # 没有获取到任何组,则采样单个获取的方法
         OOP_ORM_DI_ORM::set_query_info($obj, $finder, $config);
         return $finder->find()->current();
     }
     $batch_where = array();
     foreach ($group as $item) {
         /**
          * @var $item OOP_ORM_Data
          */
         foreach ($mapping as $k => $v) {
             $batch_where[$k][] = $item->{$v};
         }
         foreach ($where as $k => $v) {
             $batch_where[$k][] = $v;
         }
         if ($bind) {
             $batch_where[$bind][] = $item->get_data_by_field_name($bind_field, true);
         }
     }
     # 对数据进行去重处理
     $batch_where = array_map('array_unique', $batch_where);
     $return = null;
     if (1 === count($batch_where)) {
         # 只有1个条件
         $c_key = key($batch_where);
         //当前字段名
         $in = current($batch_where);
         //获取where条件
         $rs = array();
         $g = $finder->in($c_key, $in)->find();
         $obj->__orm_callback('set_key_batch_orm_group', $g);
         foreach ($g as $item) {
             $k = $item->get_data_by_field_name($c_key, true);
             $item->__orm_callback('add_parent_group_id', $group_ids);
             $rs[$k][] = $item;
         }
         foreach ($group as $item) {
             if ($mapping) {
                 $v = current($mapping);
                 $k = $item->{$v};
             } elseif ($where) {
                 $k = current($where);
             } elseif ($bind) {
                 $k = $item->get_data_by_field_name($bind_field, true);
             } else {
                 $k = null;
             }
             if (!isset($rs[$k])) {
                 if ($type !== OOP_ORM::PARAM_TYPE_O2O) {
                     $current = array();
                 } else {
                     $current = null;
                 }
             } else {
                 $current = $rs[$k];
                 if ($type === OOP_ORM::PARAM_TYPE_O2O) {
                     $current = current($current);
                 }
             }
             if ($item === $obj) {
                 $return = $current;
             } else {
                 # 通过回调设置ORM批量获取的数据
                 $item->__orm_callback('set_batch_orm_data', $this->key, $current);
             }
         }
     } else {
         foreach ($batch_where as $k => $v) {
             $finder->in($k, $v);
             $finder->group_by($k);
         }
         # 组织数据
         $rs = array();
         foreach ($finder->find() as $item) {
             /**
              * @var $item OOP_ORM_Data
              */
             $k = '';
             foreach ($mapping as $m_k => $v) {
                 $k .= ',' . $item->{$m_k};
             }
             foreach ($where as $m_value) {
                 $k .= ',' . $m_value;
             }
             if ($bind) {
                 $k .= ',' . $item->get_data_by_field_name($bind, true);
             }
             if (!isset($rs[$k])) {
                 $rs[$k] = $item;
             }
         }
         foreach ($group as $i => $item) {
             $k = '';
             foreach ($mapping as $m_key) {
                 $k .= ',' . $item->{$m_key};
             }
             foreach ($where as $m_value) {
                 $k .= ',' . $m_value;
             }
             if ($bind) {
                 $k .= ',' . $item->get_data_by_field_name($bind_field, true);
             }
             if (!isset($rs[$k])) {
                 # 有可能数据库中没有对应的数据
                 if ($type !== OOP_ORM::PARAM_TYPE_O2O) {
                     $current = array();
                 } else {
                     $current = null;
                 }
             } else {
                 $current = $rs[$k];
                 if ($type === OOP_ORM::PARAM_TYPE_O2O) {
                     $current = current($current);
                 }
             }
             if ($item === $obj) {
                 $return = $current;
             } else {
                 # 通过回调设置ORM批量获取的数据
                 $item->__orm_callback('set_batch_orm_data', $this->key, $current);
             }
         }
     }
     return $return;
 }