protected static function _get_orm_group_data(\OOP_ORM $obj, \OOP_ORM_Data $data_obj, $orm_config, $index) { $group_data = $data_obj->get_group_data(); if ($group_data) { foreach ($orm_config['mapping'] as $k => $v) { $tmpdata = array(); //记录临时数据 foreach ($group_data as $i => $item) { # 当前字段需要符合的条件 $iswhere = true; if (isset($orm_config['where'])) { foreach ($orm_config['where'] as $wk => $wv) { if ($item->{$wk} != $wv) { $iswhere = false; } } } if ($iswhere) { $tmpdata[] = $item->{$v}; } } $tmpdata = \array_unique($tmpdata); if ($tmpdata) { $obj->in($k, $tmpdata); } } if ($tmpdata) { # 如果有临时数据,则表明可以进行批量获取 if (isset($orm_config['where'])) { foreach ($orm_config['where'] as $k => $v) { # 额外的一些条件 $obj->where($k, $v); } } $all_data = $obj->find(); } else { $all_data = array(); } # 整理data,将数据放在一个以mapping条件组成的key数组里 $tmp = array(); foreach ($all_data as $item) { $tmpkey = ''; foreach ($orm_config['mapping'] as $k => $v) { $tmpkey .= '|' . $k . '_' . $item->{$k}; } $tmp[$tmpkey] = $item; } # 将获取来的数据赋值给各个对象 foreach ($group_data as $item) { $tmpkey = ''; foreach ($orm_config['mapping'] as $k => $v) { $tmpkey .= '|' . $k . '_' . $item->{$v}; } if ($item === $data_obj) { # 当前对象 $data = isset($tmp[$tmpkey]) ? $tmp[$tmpkey] : null; } else { $item->{$index} = isset($tmp[$tmpkey]) ? $tmp[$tmpkey] : null; } } if (isset($data)) { # 已经获取到当前对象,直接返回 return $data; } } return true; }
protected static function set_query_info(OOP_ORM_Data $obj, OOP_ORM $finder, $config) { # WHERE if ($config['where']) { $finder->where($config['where']); } # MAPPING if ($config['mapping']) { foreach ($config['mapping'] as $k => $v) { $finder->where($k, $obj->{$v}); } } # 绑定数据 if (isset($config['bind']) && $config['bind']) { $finder->where($config['bind'], $obj->get_data_by_field_name($config['bind'], true)); } if (isset($config['order_by']) && $config['order_by']) { foreach ($config['order_by'] as $k => $v) { $finder->order_by($k, $v); } } # OFFSET if (isset($config['offset']) && $config['offset'] > 0) { $finder->offset($config['offset']); } # GROUP BY if (isset($config['group_by']) && $config['group_by']) { foreach ($config['group_by'] as $item) { $finder->group_by($item); } } # ORDER BY if (isset($config['order_by']) && $config['order_by']) { foreach ($config['order_by'] as $key => $item) { $finder->order_by($key, $item); } } # IN if (isset($config['in']) && $config['in']) { foreach ($config['in'] as $key => $item) { $finder->in($key, $item); } } # LIMIT if (isset($config['limit']) && $config['limit']) { $finder->limit($config['limit']); } # LIKE if (isset($config['like']) && $config['like']) { $finder->like($config['like']); } # HAVING if (isset($config['having']) && $config['having']) { $finder->having($config['having']); } # 支持扩展所有的方法 if (isset($config['other']) && $config['other']) { foreach ($config['other'] as $argv) { $k = array_pop($argv); call_user_func_array(array($finder, $k), $argv); } } }