/** * 设置根节点数据 * * @param array $sub_field * @param fixed $data * @param fixed $value * @return boolean 数据是否修改 */ public static function set_sub_offset_data(&$data, $value, array $sub_field) { $first_field = array_shift($sub_field); if ($sub_field) { if (is_object($data)) { return OOP_ORM_Parse::set_sub_offset_data($data->{$first_field}, $value, $sub_field); } else { if (!is_array($data)) { $data = array(); } return OOP_ORM_Parse::set_sub_offset_data($data[$first_field], $value, $sub_field); } } else { if (is_object($data)) { $old_data = $data->{$first_field}; $data->{$first_field} = $value; $status = $old_data === $data->{$first_field} ? false : true; } else { if (!is_array($data)) { $data = array(); } $status = $value === $data[$first_field] ? false : true; $data[$first_field] = $value; } return $status; } }
/** * 创建实例化数据 * * @param string $key */ protected function _create_data($index) { # 标记为已创建 $this->_created_offset[$index] = true; $config = $this->_get_offset_config($index); if (!$config) { return false; } # 是否已经设置过数据 $offset_isset = isset($this->_offset_data[$index]); if ($offset_isset) { $offset_data = $data = $this->_offset_data[$index]; if (is_object($data) && $data instanceof stdClass) { # 将stdClass对象转成array类型 $data = (array) $data; } } else { if (isset($config['cache'])) { # 数据缓存 $data = OOP_ORM_Parse::get_offset_cache_data($this, $index, $config['cache']); if (false !== $data) { $this->_offset_data[$index] = $data; return true; } } else { $data = false; } if (null === $data || false === $data) { if (isset($config['_parent_offset_name'])) { # 映射字段 $parent_offset = $config['_parent_offset_name']; $data = OOP_ORM_Parse::get_sub_offset_data($this->{$parent_offset}, explode('.', $config['_sub_key_name']), $index); } elseif (isset($config['orm'])) { # 获取数据 $data = OOP_ORM_Parse::get_orm_data_by_config($this, $config['orm'], $index); } elseif (isset($config['data'])) { # 处理mapping if (isset($config['data']['mapping'])) { foreach ($config['data']['mapping'] as $k => $v) { $config['data']['where'][$v] = $this->{$k}; } } $data = false; # 处理缓存 if (isset($config['data']['cache'])) { $data = OOP_ORM_Parse::get_cache_data($index, @$config['data']['where'], $config['data']['cache']); } if (false === $data) { # 获取数据 $data = OOP_ORM_Parse::get_data($config['data'], $this); # 缓存数据 if (isset($config['data']['cache'])) { OOP_ORM_Parse::set_cache_data($index, @$config['data']['where'], $config['data']['cache'], $data); } } } else { $data = false; } } } // 需要返回OBJECT对象 if (isset($config['object'])) { $object_name = $config['object']['name']; if (!class_exists($object_name, true)) { throw new Exception('指定的对象:' . $object_name . '不存在!'); } if (isset($config['object']['callback']['set_data'])) { $obj = new $object_name(); $call_set_data_function = $config['object']['callback']['set_data']; $obj->{$call_set_data_function}($data); } else { $obj = new $object_name($data); } unset($data); //释放引用关系 $data = $obj; } if (!$offset_isset || isset($offset_data) && $data !== $offset_data) { $this->_offset_data[$index] = $data; } # 设置缓存 if (false !== $data && isset($config['cache'])) { OOP_ORM_Parse::set_offset_cache_data($this, $index, $config['cache'], $data); } return true; }
/** * 根据offset及配额设置缓存数据 * * @param string $index * @param array $config * @param data * * @return boolean */ public static function set_offset_cache_data(OOP_ORM_Data $data_obj, $key, $cache_config, $data) { # 获取一个key $cache_key = OOP_ORM_Parse::get_offset_cache_key($data_obj, $key); $cache = new Cache($cache_config['driver_config']); return $cache->set($cache_key, $data, isset($cache_config['expire']) ? $cache_config['expire'] : 3600, $cache_config['expire_type']); }