Exemple #1
0
 /**
  * 生成代码
  */
 function getCode($class_name, $table_name)
 {
     $arr = explode('.', $table_name);
     if (isset($arr[1])) {
         $table_name = $arr[1];
         $schema = $arr[0] . '.';
     } else {
         $table_name = $arr[0];
         $schema = '';
     }
     $dbo = QDB::getConn();
     $prefix = $dbo->getTablePrefix();
     if ($prefix && substr($table_name, 0, strlen($prefix)) == $prefix) {
         $table_name = substr($table_name, strlen($prefix));
     }
     $table_name = "{$schema}{$table_name}";
     $config = array('name' => $table_name);
     $table = new QDB_Table($config);
     $meta = $table->columns();
     $pk = array();
     foreach ($meta as $field) {
         if ($field['pk']) {
             $pk[] = $field['name'];
         }
     }
     $viewdata = array('class_name' => $class_name, 'table_name' => $table_name, 'meta' => $meta, 'pk' => $pk);
     return $this->_parseTemplate('model', $viewdata);
 }
Exemple #2
0
 /**
  * 生成指定名称的模型
  *
  * @param string $model_name
  * @param string $table_name
  * @param QDB_Adapter_Abstract $dbo
  *
  * @return QGenerator_Model
  */
 function generate($model_name, $table_name, $dbo)
 {
     $class_name = $this->_normalizeClassName($model_name);
     $dir = rtrim($this->_module->moduleDir(), '/\\') . '/model';
     $path = $this->_classFilePath($dir, $class_name);
     $this->_logClean();
     if (file_exists($path)) {
         throw new Q_ClassFileExistsException($class_name, $path);
     }
     // 确定数据表名称
     $arr = explode('.', $table_name);
     if (isset($arr[1])) {
         $table_name = $arr[1];
         $schema = $arr[0] . '.';
     } else {
         $table_name = $arr[0];
         $schema = '';
     }
     if (is_null($dbo)) {
         $dsn = Q::ini('db_dsn_pool/default');
         $dbtype = $dsn['driver'];
         $objid = "dbo_{$dbtype}_" . md5(serialize($dsn));
         $dbo_class_name = 'QDB_Adapter_' . ucfirst($dbtype);
         $dbo = new $dbo_class_name($dsn, $objid);
     }
     $prefix = $dbo->getTablePrefix();
     if ($prefix && substr($table_name, 0, strlen($prefix)) == $prefix) {
         $table_name = substr($table_name, strlen($prefix));
     }
     $table_name = "{$schema}{$table_name}";
     $config = array('name' => $table_name, 'conn' => $dbo);
     $table = new QDB_Table($config);
     $meta = $table->columns();
     $pk = array();
     foreach ($meta as $field) {
         if ($field['pk']) {
             $pk[] = $field['name'];
         }
     }
     $data = array('class_name' => $class_name, 'table_name' => $table_name, 'meta' => $meta, 'pk' => $pk);
     $content = $this->_parseTemplate('model', $data);
     $this->_createFile($path, $content);
     return $this;
 }
Exemple #3
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);
     }
 }
Exemple #4
0
 function testNextID()
 {
     $id = $this->table->nextID();
     $next_id = $this->table->nextID();
     $this->assertTrue($next_id > $id);
 }