Exemple #1
0
 /**
  * 直接添加一个关联对象
  *
  * @param QDB_ActiveRecord_Abstract $source
  * @param QDB_ActiveRecord_Abstract $target
  *
  * @return QDB_ActiveRecord_Association_Abstract
  */
 function addRelatedObject(QDB_ActiveRecord_Abstract $source, QDB_ActiveRecord_Abstract $target)
 {
     $this->init();
     $target->changePropForce($this->target_key, $source->{$this->source_key});
     $target->save(0, $this->on_save);
     return $this;
 }
 function __construct(QDB_ActiveRecord_Abstract $object)
 {
     $this->ar_object = $object;
     $class_name = $object->getMeta()->class_name;
     // LC_MSG: Destroy object "%s" instance without ID.
     parent::__construct($class_name, __('Destroy object "%s" instance without ID.', $class_name));
 }
Exemple #3
0
 /**
  * 在数据库中创建 ActiveRecord 对象前调用
  *
  * @param QDB_ActiveRecord_Abstract $obj
  */
 function _before_create(QDB_ActiveRecord_Abstract $obj)
 {
     if ($this->_settings['gen2char']) {
         $new_id = ariUUID();
         $idname = reset($this->_meta->idname);
         $obj->changePropForce($idname, $new_id);
     } else {
         $new_id = self::genUUID($this->_settings['genorder'], $this->_settings['being_timestamp'], $this->_settings['suffix_len']);
         $idname = reset($this->_meta->idname);
         $obj->changePropForce($idname, $new_id);
     }
 }
 /**
  * 添加和一个对象的关联关系
  *
  * @param QDB_ActiveRecord_Abstract $source
  * @param QDB_ActiveRecord_Abstract $target
  *
  * @return QDB_ActiveRecord_Association_ManyToMany
  */
 function bindRelatedObject(QDB_ActiveRecord_Abstract $source, QDB_ActiveRecord_Abstract $target)
 {
     $this->init();
     if ($this->mid_meta) {
     } else {
         $conn = $this->mid_table->getConn();
         $target->save($this->on_save);
         $source_key_value = $source->{$this->source_key};
         $target_key_value = $target->{$this->target_key};
         $sql = sprintf('SELECT COUNT(*) FROM %s WHERE %s = %s AND %s = %s', $conn->qid($this->mid_table->getFullTableName()), $conn->qid($this->mid_source_key), $conn->qstr($source_key_value), $conn->qid($this->mid_target_key), $conn->qstr($target_key_value));
         if (intval($conn->getOne($sql)) < 1) {
             $this->mid_table->insert(array($this->mid_source_key => $source_key_value, $this->mid_target_key => $target_key_value));
         }
     }
     return $this;
 }
 protected function _checkUniqueness(QDB_ActiveRecord_Abstract $obj, QDB_Cond $more_cond = null, $ignore_id = false)
 {
     $check_props = Q::normalize($this->_settings['check_props']);
     if (empty($check_props)) {
         return;
     }
     $checks = array();
     $error = array();
     foreach ($check_props as $check) {
         if ($ignore_id && $check == $obj->idname()) {
             continue;
         }
         if (strpos($check, '+') !== false) {
             $props = Q::normalize($check, '+');
             $cond = array();
             foreach ($props as $prop_name) {
                 $cond[$prop_name] = $obj->{$prop_name};
             }
         } else {
             $cond = array($check => $obj->{$check});
             $props = $check;
         }
         if (!is_null($more_cond)) {
             $cond[] = $more_cond;
         }
         $test = $this->_meta->find($cond)->count()->query();
         if ($test['row_count'] < 1) {
             continue;
         }
         if (isset($this->_settings['error_messages'][$check])) {
             $error[$check] = array($check => $this->_settings['error_messages'][$check]);
         } else {
             $error[$check] = array($check => "{$check} duplicated");
         }
     }
     if (!empty($error)) {
         throw new QDB_ActiveRecord_ValidateFailedException($error, $obj);
     }
 }
 /**
  * 构造函数
  *
  * @param array $errors
  * @param QDB_ActiveRecord_Abstract $obj
  */
 function __construct(array $errors, QDB_ActiveRecord_Abstract $obj)
 {
     $this->validate_obj = $obj;
     parent::__construct($errors, $obj->toArray(0));
 }
Exemple #7
0
 /**
  * 在保存新建用户对象失败抛出异常时,还原用户的密码属性
  *
  * @param QDB_ActiveRecord_Abstract $member 保存出错的用户对象
  * @param Exception $ex 异常
  */
 function _save_exception_handler(QDB_ActiveRecord_Abstract $member, Exception $ex)
 {
     if (isset($this->_saved_state['password'])) {
         $member->changePropForce($this->_settings['password_prop'], $this->_saved_state['password']);
         unset($this->_saved_state['password']);
     }
 }
Exemple #8
0
 /**
  * 获得用户的 ACL 数据
  *
  * @param QDB_ActiveRecord_Abstract $obj
  * @param string $acldata_props
  *
  * @return array
  */
 function getAclData(QDB_ActiveRecord_Abstract $obj, $acldata_props = null)
 {
     if (is_null($acldata_props)) {
         $acldata_props = $this->_settings['acldata_props'];
     }
     $acldata_props = Q::normalize($acldata_props);
     $data = array();
     foreach ($acldata_props as $pn) {
         $data[$pn] = $obj->{$pn};
     }
     $data['id'] = $obj->id();
     return $data;
 }
Exemple #9
0
 /**
  * 执行上传文件
  *
  * @param QDB_ActiveRecord_Abstract $obj
  */
 function _exec_upload(QDB_ActiveRecord_Abstract $obj)
 {
     if (empty($this->_settings['upload_config'])) {
         return;
     }
     $uploader = new Helper_Uploader();
     $error = array();
     foreach ($this->_settings['upload_config'] as $file_id => $config) {
         //必需
         $post_key = isset($config['post_key']) ? $config['post_key'] : $file_id;
         if (!$uploader->existsFile($post_key)) {
             if (isset($config['required']) && $config['required']) {
                 if ($obj->id() && !empty($obj->{$file_id})) {
                     $obj->willChanged($file_id);
                 } else {
                     $error[$post_key]['required'] = $config['errmsgs']['required'];
                 }
             }
             continue;
         }
         $file = $uploader->file($post_key);
         $filename = $file->filename();
         $extname = $file->extname();
         // 验证文件类型
         if (isset($config['allowed_filetypes']) && $config['allowed_filetypes']) {
             if (!$file->isValid($config['allowed_filetypes'])) {
                 $error[$post_key]['allowed_filetypes'] = $config['errmsgs']['allowed_filetypes'];
                 continue;
             }
         } else {
             if ($file->isValid('.php')) {
                 $error[$post_key]['allowed_filetypes'] = "PHP 文件是不允许上传的.";
                 continue;
             }
         }
         //验证文件大小
         if (isset($config['max_size']) && $config['max_size'] > 0) {
             if ($file->filesize() > $config['max_size'] * 1024) {
                 $error[$post_key]['max_size'] = $config['errmsgs']['max_size'];
                 continue;
             }
         }
         // 验证图片尺寸
         if (isset($config['image_dimension']) && $config['image_dimension']) {
             list($width, $height, $type, $attr) = getimagesize($file->filepath());
             if (!$width || !$height) {
                 continue;
             }
             list($dim_width, $dim_height) = explode('*', $config['image_dimension']);
             if (isset($dim_width) && $dim_width > 0 && $dim_width != $width || isset($dim_height) && $dim_height > 0 && $dim_height != $height) {
                 $error[$post_key]['image_dimension'] = $config['errmsgs']['image_dimension'];
                 continue;
             }
         }
         $dir = rtrim($config['upload_dir'], '/\\') . DS;
         $date = date('Y-m');
         $dest_dir = $dir . $date;
         Helper_Filesys::mkdirs($dest_dir);
         $md5 = md5($filename . '-' . microtime(true));
         $_prop_filename = '';
         //如果上图片
         if (isset($config['is_image']) && $config['is_image'] || isset($config['large']) || isset($config['thumb'])) {
             $pic_filename = $md5 . '.jpg';
             $image = Helper_Image::createFromFile($file->filepath(), $file->extname());
             // 生成大图
             if (isset($config['large'])) {
                 list($w, $h) = explode('*', $config['large']);
                 $image->crop($w, $h);
             }
             $image->saveAsJpeg($dest_dir . DS . $pic_filename);
             $_prop_filename = "{$date}/{$pic_filename}";
             // 生成缩略图
             if (isset($config['thumb'])) {
                 $thumb_filename = $md5 . '-thumb.jpg';
                 list($w, $h) = explode('*', $config['thumb']);
                 $image->crop($w, $h);
                 $image->saveAsJpeg($dest_dir . DS . $thumb_filename);
             }
             // 销毁图片资源并删除上传文件
             $image->destroy();
             $file->unlink();
         } else {
             $file_name = $md5 . '.' . $extname;
             $file->move($dest_dir . DS . $file_name);
             $_prop_filename = "{$date}/{$file_name}";
         }
         //如果是更新,删除原有的
         if ($obj->id() && !empty($obj->{$file_id})) {
             $this->_delete_upload_file($obj, $file_id);
         }
         //向属性负值
         $obj->{$file_id} = $_prop_filename;
     }
     if (!empty($error)) {
         throw new QDB_ActiveRecord_ValidateFailedException($error, $obj);
     }
 }