/** * 获取处理字段di控制反转对象 * * @param $class_name * @param $key * @return OOP_ORM_DI */ protected function _get_di_by_key($key) { if ($this->_is_temp_instance) { # 临时ORM对象 if (!isset($this->_temp_di[$key])) { $config = array('is_temp_instance' => true); if (!$this->_expand_key || $this->_expand_key === $key || array_key_exists($key, $this->_data)) { $class = 'OOP_ORM_DI_Default'; $config['field_name'] = $key; } else { # 自动扩展 $class = 'OOP_ORM_DI_Virtual'; $config['parent_offset'] = $this->_expand_key; $config['sub_offsets'] = array($key); } $this->_temp_di[$key] = new $class($this->_class_name, $key, $this->tablename(), $config); } return $this->_temp_di[$key]; } else { return OOP_ORM_DI::get_class_di($this->_class_name, $key); } }
/** * 获取当前类型的数据 * * @param OOP_ORM_Data $obj * @param $data * @param $compiled_data * @return mixed */ public function &get_data(OOP_ORM_Data $obj, &$data, &$compiled_data, &$compiled_raw_data) { # 处理URL变量 $this->url = $this->config['resource']; if (preg_match_all('#{{([a-z0-9_]+)}}#i', $this->url, $m)) { foreach ($m[1] as $v) { $this->url = str_replace('{{' . $v . '}}', $obj->{$v}, $this->url); } } # 获取缓存 if (isset($this->config['cache'])) { /** * @var $cache Cache */ list($cache, $key) = $this->get_cache_instance_and_key($obj); $tmp_data = $cache->get($key); } else { $tmp_data = null; } # 获取内容 if (null === $tmp_data || false === $tmp_data) { $tmp_data = HttpClient::factory()->get($this->url)->data(); if (false === $tmp_data) { return false; } # 处理数据类型 if (isset($this->config['field_type'])) { OOP_ORM_DI::_check_field_type($this->config['field_type'], $tmp_data); } # 设置缓存 if (isset($this->config['cache'])) { $cache->set($key, $tmp_data, isset($this->config['cache']['expired']) ? $this->config['cache']['expired'] : 3600, isset($this->config['cache']['expire_type']) ? $this->config['cache']['expire_type'] : null); } } # 处理格式化数据 if (isset($this->config['format'])) { OOP_ORM_DI::_do_de_format_data($this->config['format'], $tmp_data); } $compiled_data[$this->key] = $tmp_data; $compiled_raw_data[$this->key] = $tmp_data; return $compiled_data[$this->key]; }
/** * 获取当前ORM的主键 * * [!!] 其中key是对象的key,value是字段名 * * @return array */ public function get_pk_name() { return OOP_ORM_DI::get_pk_name_by_class_name(strtolower($this->get_orm_name('data'))); }
protected function get_metadata_by_group_data($config, $metadata) { if ($config['depth'] > 0) { $tmp_data = array(); foreach ($metadata as $item) { if ($item['field_name'] !== $config['field_name']) { continue; } if (strpos($item['meta_index'], '.') !== false) { // 构造一个多维数组 $tmp =& $tmp_data; foreach (explode('.', $item['meta_index']) as $key) { $tmp =& self::_create_metadata_tree($key, $tmp); } $tmp = $item['meta_value']; if (isset($config['format']) && $config['format']) { OOP_ORM_DI::_do_de_format_data($config['format'], $tmp); } unset($tmp); } else { if (isset($config['format']) && $config['format']) { OOP_ORM_DI::_do_de_format_data($config['format'], $item['meta_value']); } $tmp_data[$item['meta_index']] = $item['meta_value']; } } return $tmp_data; } else { foreach ($metadata as $item) { if ($item['field_name'] === $config['field_name']) { if (isset($config['format']) && $config['format']) { OOP_ORM_DI::_do_de_format_data($config['format'], $item['meta_value']); } return $item['meta_value']; } } return null; } }
/** * 设置数据 * * @param OOP_ORM_Data $obj * @param $data * @param $compiled_data * @param $new_value * @param bool $has_compiled * @return bool */ public function set_data(OOP_ORM_Data $obj, &$data, &$compiled_data, &$compiled_raw_data, $new_value, $has_compiled) { if ($has_compiled) { if ($this->is_readonly()) { # 只读字段 return false; } } # 处理数据类型 if (isset($this->config['field_type'])) { OOP_ORM_DI::_check_field_type($this->config['field_type'], $new_value); } $compiled_data[$this->key] = $new_value; if (!$has_compiled) { $compiled_raw_data[$this->key] = $compiled_data[$this->key]; if ($this->field_name) { # 未构造完,给data设置值 $data[$this->field_name] = $this->get_data($obj, $data, $compiled_data, $compiled_raw_data); } } return true; }
/** * 返回指定对象所以的key * * 如果没有主键则返回空数组 `array()` * * @param $class_name * @return array */ public static function get_all_keys($class_name) { if (!isset(OOP_ORM_DI::$ALL_KEYS[$class_name])) { OOP_ORM_DI::parse_offset($class_name); } return OOP_ORM_DI::$ALL_KEYS[$class_name]; }
/** * 加载对应数据库所有元数据 * * @param OOP_ORM_Data $obj * @return $this * @throws Exception */ public function load_all_metadata(OOP_ORM_Data $obj) { $class_name = $obj->class_name(); $meta_table_of_key = OOP_ORM_DI::get_meta_table_of_key($class_name); $meta_group_of_key = OOP_ORM_DI::get_meta_group_of_key($class_name); if (!$meta_table_of_key) { # 没有元数据 return $this; } # 没有主键 if (!$obj->pk()) { return $this; } $keys_of_table = array(); foreach ($meta_table_of_key as $key => $table) { $keys_of_table[$table][] = $key; } $data = array(); $groups_of_table = array(); # 读数据 foreach ($keys_of_table as $table => $keys) { foreach ($this->load_metadata($obj, $table) as $hash => $item) { $data[$table][$item['meta_group']][$hash] = $item; } foreach ($keys as $key) { $group = $meta_group_of_key[$key]; $groups_of_table[$table][$group] = $group; } } foreach ($groups_of_table as $table => $groups) { foreach ($groups as $group) { # 预置空数据 $obj->__orm_callback('set_metadata', $table, $group, array()); } } foreach ($data as $table => $group_data) { foreach ($group_data as $group => $item) { $obj->__orm_callback('set_metadata', $table, $group, $item); } } return $this; }