Esempio n. 1
0
 protected function format_field_value(OOP_ORM_Data $obj, &$data, $new_data, $format_type)
 {
     # 数据主键
     $id = $obj->pk(',');
     # 这个是对应主表的表名称,它和元数据的 table_name 字段是对应的
     $table = $obj->finder()->tablename();
     # 这个是metadata所在的表名称
     $my_table = $this->table_name;
     $meta_group = $this->meta_group;
     if (null === $new_data || '' === $new_data || array() === $new_data) {
         # 删除了
         # 读取已经加载的所有元数据
         $old_metadata = $obj->__orm_callback('get_metadata');
         if (isset($old_metadata[$my_table][$meta_group])) {
             foreach ($old_metadata[$my_table][$meta_group] as $hash => $item) {
                 # 遍历所有当前组的数据
                 if ($item['table_name'] === $table && $item['field_name'] === $this->field_name) {
                     # 把当前字段的数据全部设置成 null 以便进行删除处理
                     $data[$my_table][$hash] = null;
                 }
             }
         }
         return;
     }
     # 读取已经加载的所有元数据
     $old_metadata = $obj->__orm_callback('get_metadata');
     $old_item_data = array();
     if (isset($old_metadata[$my_table][$meta_group])) {
         foreach ($old_metadata[$my_table][$meta_group] as $hash => $item) {
             # 把数据整理到一个数组里
             if ($item['table_name'] === $table && $item['field_name'] === $this->field_name) {
                 # 把当前字段的数据全部设置成 null 以便进行删除处理
                 $old_item_data[$hash] = $item;
             }
         }
     }
     # 读取新数据
     if ($this->config['depth']) {
         # 处理多行数据
         $new_item_data = array();
         foreach ($new_data as $k => $v) {
             $tmp = $this->get_meta_item($table, $id, $k, $v);
             $new_item_data[$tmp['hash']] = $tmp;
         }
     } else {
         $tmp = $this->get_meta_item($table, $id, 0, $new_data);
         $new_item_data[$tmp['hash']] = $tmp;
     }
     foreach ($new_item_data as $hash => $item) {
         $tmp_value = $item['meta_value'];
         if ($tmp_value !== array() && null !== $tmp_value) {
             if ($format_type) {
                 # 动态格式化
                 if (is_array($tmp_value) || is_object($tmp_value)) {
                     $tmp_value = serialize($tmp_value);
                 }
             } elseif (isset($this->config['format']) && $this->config['format']) {
                 # 格式化数据
                 $this->_format_data($tmp_value);
             }
         }
         $tmp_value = (string) $tmp_value;
         if ('' === $tmp_value) {
             # 空字符串,则移除 null
             unset($new_item_data[$hash]);
         } else {
             $new_item_data[$hash]['meta_value'] = $tmp_value;
             if (isset($old_item_data[$hash]) || array_key_exists($hash, $old_item_data)) {
                 if ($old_item_data[$hash]['meta_value'] === $tmp_value) {
                     # 相同数据,全部去掉掉则不更新
                     unset($new_item_data[$hash]);
                     unset($old_item_data[$hash]);
                 } else {
                     unset($old_item_data[$hash]);
                 }
             }
         }
     }
     # 把老数据清理掉
     if ($old_item_data) {
         foreach ($old_item_data as $hash => $item) {
             $new_item_data[$hash] = null;
         }
     }
     $new_data = $new_item_data;
     if (isset($data[$my_table])) {
         $data[$my_table] += $new_data;
     } else {
         $data[$my_table] = $new_data;
     }
 }
Esempio n. 2
0
 /**
  * 加载对应数据库所有元数据
  *
  * @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;
 }