Example #1
0
 function __construct()
 {
     if (null === $this->tablename) {
         throw new Exception(__('orm tablename is not declared.'));
     }
     parent::__construct();
 }
Example #2
0
 function __construct()
 {
     if (null === $this->api_url) {
         throw new \Exception(\__('orm api url is not declared.'));
     }
     parent::__construct();
 }
Example #3
0
 function __construct($api_url = null)
 {
     if (null !== $api_url) {
         $this->api_url = $api_url;
     }
     if (null === $this->api_url) {
         throw new Exception(__('orm api_url is not declared.'));
     }
     parent::__construct();
 }
Example #4
0
 function __construct($table_name = null, $database = null)
 {
     if (null !== $table_name) {
         $this->tablename = $table_name;
     }
     if (null !== $database) {
         $this->database = $database;
     }
     if (null === $this->tablename) {
         throw new Exception(__('ORM :orm tablename is not declared.', array(':orm' => get_class($this))));
     }
     parent::__construct();
 }
Example #5
0
 /**
  * 获取指定的对象,如果没有数据则尝试从资源中创建数据
  *
  * 无可用数据则返回false
  *
  * @param $group_id
  * @param $index
  * @return OOP_ORM_Data|null
  */
 protected function get_offset_data($index)
 {
     if (!isset($this->data[$index])) {
         if (!$this->resource || !$this->finder) {
             return null;
         }
         # 从数据库中获取
         $data = $this->resource->offsetGet($index);
         # 返回是null,则seek看是否对应指针数据不存在
         if (null === $data && false === $this->resource->seek($index)) {
             return null;
         }
         # 使用获取的数据创建新对象
         $this->data[$index] = $this->finder->create($data, isset($this->option['is_field_key']) ? $this->option['is_field_key'] : true, $this->id);
         if ($this->option['count'] == count($this->data)) {
             # 获取完所有数据后就可以直接释放资源,不用等到对象被消毁
             $this->release_resource();
             # 重新排序,避免排序错乱
             ksort($this->data, SORT_NUMERIC);
         }
     }
     return $this->data[$index];
 }
Example #6
0
 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;
 }
Example #7
0
 /**
  * 设置为延迟获取数据
  *
  * @return string
  */
 protected function __orm_callback_set_finder(OOP_ORM $finder)
 {
     if (!$this->_finder_id) {
         $this->_finder_id = uniqid('f');
     }
     OOP_ORM_Data::$FINDERS[$this->_finder_id] = $finder;
     $driver = $finder->driver();
     if ($driver instanceof Database) {
         $this->_is_support_object_value = $driver->driver()->is_support_object_value();
     } elseif ($driver instanceof HttpClient) {
         $this->_is_support_object_value = true;
     }
 }
Example #8
0
 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);
         }
     }
 }