Example #1
0
 /**
  * 创建实例化数据
  *
  * @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;
 }