Example #1
0
 /**
  * 获取组的所有数据
  *
  * @return array
  */
 public function get_group_data($group_id = null)
 {
     if ($group_id) {
         return OOP_ORM_Result::get_data_by_group_id($group_id);
     }
     $data = array();
     foreach ($this->_group_ids as $gid => $t) {
         if ($rs = OOP_ORM_Result::get_data_by_group_id($gid)) {
             $data = array_merge($data, $rs);
         }
     }
     if (!$group_id && !in_array($this, $data)) {
         $data[] = $this;
     }
     return $data;
 }
Example #2
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;
 }