Beispiel #1
0
 /**
  * 第一步初始化
  *
  * @param string $class
  */
 protected function _init1($class)
 {
     // 从指定类获得初步的定义信息
     Q::loadClass($class);
     $this->class_name = $class;
     $ref = (array) call_user_func(array($class, '__define'));
     /**
      * 检查是否是继承
      */
     if (!empty($ref['inherit'])) {
         $this->inherit_base_class = $ref['inherit'];
         /**
          * 继承类的 __define() 方法只需要指定与父类不同的内容
          */
         $base_ref = (array) call_user_func(array($this->inherit_base_class, '__define'));
         $ref = array_merge_recursive($base_ref, $ref);
     }
     // 被继承的类
     $this->inherit_type_field = !empty($ref['inherit_type_field']) ? $ref['inherit_type_field'] : null;
     // 设置表数据入口对象
     $table_config = !empty($ref['table_config']) ? (array) $ref['table_config'] : array();
     if (!empty($ref['table_class'])) {
         $this->table = $this->_tableByClass($ref['table_class'], $table_config);
     } else {
         $this->table = $this->_tableByName($ref['table_name'], $table_config);
     }
     $this->table_meta = $this->table->columns();
     // 根据字段定义确定字段属性
     if (empty($ref['props']) || !is_array($ref['props'])) {
         $ref['props'] = array();
     }
     foreach ($ref['props'] as $prop_name => $config) {
         $this->addProp($prop_name, $config);
     }
     // 将没有指定的字段也设置为对象属性
     foreach ($this->table_meta as $prop_name => $field) {
         if (isset($this->props2fields[$prop_name])) {
             continue;
         }
         $this->addProp($prop_name, $field);
     }
     // 设置其他选项
     if (!empty($ref['create_reject'])) {
         $this->create_reject = array_flip(Q::normalize($ref['create_reject']));
     }
     if (!empty($ref['update_reject'])) {
         $this->update_reject = array_flip(Q::normalize($ref['update_reject']));
     }
     if (!empty($ref['create_autofill']) && is_array($ref['create_autofill'])) {
         $this->create_autofill = $ref['create_autofill'];
     }
     if (!empty($ref['update_autofill']) && is_array($ref['update_autofill'])) {
         $this->update_autofill = $ref['update_autofill'];
     }
     if (!empty($ref['attr_accessible'])) {
         $this->attr_accessible = array_flip(Q::normalize($ref['attr_accessible']));
     }
     if (!empty($ref['attr_protected'])) {
         $this->attr_protected = array_flip(Q::normalize($ref['attr_protected']));
     }
     // 准备验证规则
     if (empty($ref['validations']) || !is_array($ref['validations'])) {
         $ref['validations'] = array();
     }
     $this->validations = $this->_prepareValidationRules($ref['validations']);
     // 设置对象 ID 属性名
     $pk = $this->table->getPK();
     $this->idname = array();
     foreach ($this->table->getPK() as $pk) {
         $pn = $this->fields2props[$pk];
         $this->idname[$pn] = $pn;
     }
     $this->idname_count = count($this->idname);
     // 绑定行为插件
     if (isset($ref['behaviors'])) {
         $config = isset($ref['behaviors_settings']) ? $ref['behaviors_settings'] : array();
         $this->bindBehaviors($ref['behaviors'], $config);
     }
 }