Example #1
0
 function __construct($config_name = 'default', $udi = null, $model = null, $has_file = null, $must = false)
 {
     parent::__construct('form_' . $config_name, url($udi), 'post');
     if ($has_file) {
         $this->enctype = self::ENCTYPE_MULTIPART;
         $this->has_file = $has_file;
         $this->must = $must;
     }
     $file_name = $config_name . '_form.yaml';
     $filename = rtrim(dirname(__FILE__), '/\\') . DS . $file_name;
     $this->loadFromConfig(Helper_YAML::loadCached($filename));
     $this->addValidations(QDB_ActiveRecord_Meta::instance($model));
 }
Example #2
0
 /**
  * 构造函数
  *
  * @param array $data
  * @param int $names_style
  * @param boolean $from_storage
  */
 function __construct(array $data = null, $names_style = QDB::PROP, $from_storage = false)
 {
     $this->_class_name = get_class($this);
     if (!isset(self::$_meta[$this->_class_name])) {
         self::$_meta[$this->_class_name] = QDB_ActiveRecord_Meta::instance($this->_class_name);
     }
     $meta = self::$_meta[$this->_class_name];
     /* @var $meta QDB_ActiveRecord_Meta */
     // 设置对象属性默认值
     $this->_props = $meta->default_props;
     if (is_array($data)) {
         $this->setProps($data, $names_style, null, $from_storage);
     }
     // 触发 after_initialize 事件
     $this->_after_initialize();
     $this->_event(self::AFTER_INITIALIZE);
     $this->_after_initialize_post();
 }
 function init()
 {
     if ($this->_inited) {
         return $this;
     }
     parent::init();
     $p = $this->_init_config;
     if (empty($p['mid_class'])) {
         // 如果没有指定中间表对应的 ActiveRecord 类,则使用表数据入口直接处理中间表
         $this->mid_meta = null;
         if (empty($p['mid_table_name'])) {
             // 尝试自动确定中间表名称
             $t1 = $this->source_meta->table->name;
             $t2 = $this->target_meta->table->name;
             if ($t1 <= $t2) {
                 $mid_table_name = $t1 . '_has_' . $t2;
             } else {
                 $mid_table_name = $t2 . '_has_' . $t1;
             }
         } else {
             $mid_table_name = $p['mid_table_name'];
         }
         $this->mid_table = new QDB_Table(array('name' => $mid_table_name));
     } else {
         // 如果中间表作为实体,则由指定的 ActiveRecord 继承类负责处理中间表
         $this->mid_meta = QDB_ActiveRecord_Meta::instance($p['mid_class']);
         $this->mid_table = $this->mid_meta->table;
     }
     $this->source_key = !empty($p['source_key']) ? $p['source_key'] : reset($this->source_meta->idname);
     $this->target_key = !empty($p['target_key']) ? $p['target_key'] : reset($this->target_meta->idname);
     $this->mid_source_key = !empty($p['mid_source_key']) ? $p['mid_source_key'] : reset($this->source_meta->idname);
     $this->mid_target_key = !empty($p['mid_target_key']) ? $p['mid_target_key'] : reset($this->target_meta->idname);
     $this->mid_mapping_to = !empty($p['mid_mapping_to']) ? $p['mid_mapping_to'] : 'mid_data';
     $class = $this->target_meta->class_name;
     // $this->source_meta->addDynamicMethod("bind{$class}", array($this, 'bindTarget'));
     // $this->source_meta->addDynamicMethod("unbind{$class}", array($this, 'unbindTarget'));
     unset($this->_init_config);
     return $this;
 }
Example #4
0
 /**
  * 返回当前 ActiveRecord 类的元数据对象
  *
  * @static
  *
  * @return QDB_ActiveRecord_Meta
  */
 static function meta()
 {
     return QDB_ActiveRecord_Meta::instance(__CLASS__);
 }
Example #5
0
 /**
  * 设置一个属性的 setter 方法
  *
  * @param string $prop_name
  * @param callback $callback
  * @param array $custom_parameters
  */
 protected function _setPropSetter($prop_name, $callback, $custom_parameters = array())
 {
     $this->_meta->setPropSetter($prop_name, $callback, $custom_parameters);
     $this->_setters[] = $prop_name;
 }
Example #6
0
 /**
  * 回调函数,用于分析查询中包含的关联表名称
  *
  * @param string $table_name
  *
  * @return string
  */
 function _parseTableName($table_name)
 {
     if (strpos($table_name, '.') !== false) {
         list($schema, $table_name) = explode('.', $table_name);
     } else {
         $schema = null;
     }
     if (is_null($this->_meta) || !isset($this->_meta->associations[$table_name])) {
         return $table_name;
     }
     $assoc = $this->_meta->getAssoc($table_name)->init();
     $target_table = $assoc->target_meta->table;
     if ($schema && $target_table->schema && $target_table->schema != $schema) {
         return "{$schema}.{$table_name}";
     }
     $assoc_table_name = $assoc->target_meta->table->getFullTableName();
     $current_table_name = $this->_getCurrentTableName();
     switch ($assoc->type) {
         case QDB::HAS_MANY:
         case QDB::HAS_ONE:
         case QDB::BELONGS_TO:
             $key = "{$assoc->type}-{$assoc_table_name}";
             if (!isset($this->_joined_tables[$key])) {
                 $this->joinInner($assoc_table_name, '', "{$assoc_table_name}.{$assoc->target_key} = " . "{$current_table_name}.{$assoc->source_key}");
                 $this->_joined_tables[$key] = true;
             }
             break;
         case QDB::MANY_TO_MANY:
             $mid_table_name = $assoc->mid_table->getFullTableName();
             $key = "{$assoc->type}-{$mid_table_name}";
             if (!isset($this->_joined_tables[$key])) {
                 $this->joinInner($mid_table_name, '', "{$mid_table_name}.{$assoc->mid_source_key} = " . "{$current_table_name}.{$assoc->source_key}");
                 $this->joinInner($assoc_table_name, '', "{$assoc_table_name}.{$assoc->target_key} = " . "{$mid_table_name}.{$assoc->mid_target_key}");
                 $this->_joined_tables[$key] = true;
             }
             break;
     }
     return $assoc_table_name;
 }
Example #7
0
 /**
  * 回调函数,用于分析查询中包含的关联表名称
  *
  * @param string $table_name
  *
  * @return string
  */
 function _parseTableName($table_name)
 {
     if (strpos($table_name, '.') !== false) {
         list($schema, $table_name) = explode('.', $table_name);
     } else {
         $schema = null;
     }
     if (is_null($this->_meta) || !isset($this->_meta->associations[$table_name])) {
         return $table_name;
     }
     $assoc = $this->_meta->assoc($table_name)->init();
     $target_table = $assoc->target_meta->table;
     if ($schema && $target_table->schema && $target_table->schema != $schema) {
         return "{$schema}.{$table_name}";
     }
     $assoc_table_name = $assoc->target_meta->table->getFullTableName();
     $current_table_name = $this->_getCurrentTableName();
     switch ($assoc->type) {
         case QDB::HAS_MANY:
         case QDB::HAS_ONE:
         case QDB::BELONGS_TO:
             $key = "{$assoc->type}-{$assoc_table_name}";
             if (!isset($this->_joined_tables[$key])) {
                 // 支持额外join 条件设定,用于关联查询
                 $join_cond_extra = '';
                 if (isset($this->_meta->props[$assoc->mapping_name]['assoc_params']['join_cond_extra'])) {
                     $join_cond_extra = " AND " . trim($this->_meta->props[$assoc->mapping_name]['assoc_params']['join_cond_extra']);
                 }
                 $this->joinInner($assoc_table_name, '', "[{$assoc_table_name}.{$assoc->target_key}] = " . "[{$current_table_name}.{$assoc->source_key}] {$join_cond_extra}");
                 $this->_joined_tables[$key] = true;
             }
             break;
         case QDB::MANY_TO_MANY:
             $mid_table_name = $assoc->mid_table->getFullTableName();
             $key = "{$assoc->type}-{$mid_table_name}";
             if (!isset($this->_joined_tables[$key])) {
                 $this->joinInner($mid_table_name, '', "[{$mid_table_name}.{$assoc->mid_source_key}] = " . "[{$current_table_name}.{$assoc->source_key}]");
                 $this->joinInner($assoc_table_name, '', "[{$assoc_table_name}.{$assoc->target_key}] = " . "[{$mid_table_name}.{$assoc->mid_target_key}]");
                 $this->_joined_tables[$key] = true;
             }
             break;
     }
     return $assoc_table_name;
 }
Example #8
0
 /**
  * 初始化关联
  *
  * @return QDB_ActiveRecord_Association_Abstract
  */
 function init()
 {
     if (!$this->_inited) {
         $this->target_meta = QDB_ActiveRecord_Meta::instance($this->_init_config['target_class']);
         $this->source_key_alias = $this->mapping_name . '_source_key';
         $this->target_key_alias = $this->mapping_name . '_target_key';
         $this->_inited = true;
     }
     return $this;
 }
Example #9
0
 /**
  * 从模型载入验证规则
  *
  * @param array|string $models
  *
  * @return QForm
  */
 function loadValidationsFromModel($models)
 {
     $models = Q::normalize($models);
     foreach ($models as $model_class) {
         $meta = QDB_ActiveRecord_Meta::instance($model_class);
         $validations = $meta->getAllValidation();
         foreach ($validations as $id => $policy) {
             $this->appendValidation($id, $policy['rules']);
         }
     }
     return $this;
 }