Exemplo n.º 1
0
 public function buildAction()
 {
     $this->disableRenderView();
     $this->ajaxGo($this->view->url(array('action' => $this->z_defaultAction, 'id' => NULL)));
     $id = $this->_getParam('id');
     $tableModel = $this->z_model;
     $fieldModel = new Z_Model_Dbtablesfields();
     $tableObject = $tableModel->find($id)->current();
     $fieldObjects = $fieldModel->fetchAll(array('dbtable_id=?' => $id), 'orderid asc');
     if ($fieldObjects->count() == 0) {
         Z_FlashMessenger::addMessage('Не заданы поля для таблицы');
         return;
     }
     //генерация запроса на создание таблицы
     $queryString = 'CREATE TABLE  `' . $tableObject->title . '` ( `id` INT( 11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,';
     $i = 0;
     foreach ($fieldObjects as $field) {
         $i++;
         if ($field->title == 'id') {
             continue;
         }
         $addstr = '`' . $field->title . '`';
         $addstr .= ' ' . strtoupper($field->type);
         $addstr .= $field->len ? '(' . $field->len . ')' : '';
         $addstr .= $field->is_null ? ' NULL' : ' NOT NULL';
         $addstr .= $field->default == 'asdefine' ? " DEFAULT '" . $field->default_value . "'" : '';
         if ($i < $fieldObjects->count()) {
             $addstr .= ' ,';
         }
         $queryString .= $addstr;
     }
     foreach ($fieldObjects as $field) {
         if ($field->is_index) {
             $queryString .= ' , INDEX (`' . $field->title . '`)';
         }
     }
     //выполнение запроса
     $tableExists = Z_Db_Table::getDefaultAdapter()->listTables();
     $queryString .= ') ENGINE = INNODB CHARACTER SET utf8 COLLATE utf8_general_ci';
     if (!in_array($tableObject->title, $tableExists)) {
         Z_Db_Table::getDefaultAdapter()->query($queryString);
     } else {
         Z_FlashMessenger::addMessage('Таблица "' . $tableObject->title . '" уже существует');
     }
     //генерация модели
     Z_Db_Model_Generator::generate($tableObject->title);
 }
Exemplo n.º 2
0
 /**
  * Генерирует класс модели
  * @param string $className
  * Название класса (без префикса)
  * @param string $tableName
  * Название таблицы в БД
  * @param array $params
  * Параметры для переопределения настроек по умолчанию
  * @return string
  */
 public static function generate($className, $tableName = NULL, $params = array())
 {
     if (strpos($className, 'z_') === 0 && Z_Auth::getInstance()->getUser()->getRole() == 'root') {
         $path_prefix = APPLICATION_PATH . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'library' . DIRECTORY_SEPARATOR . 'Z' . DIRECTORY_SEPARATOR . 'Model';
         self::$_classPrefix = isset($params['prefixz']) ? $params['prefixz'] : self::$_classPrefixZ;
     } else {
         $path_prefix = APPLICATION_PATH . DIRECTORY_SEPARATOR . 'models';
         self::$_classPrefix = isset($params['prefix']) ? $params['prefix'] : self::$_classPrefix;
     }
     if ($tableName == NULL) {
         $tableName = strtolower($className);
     }
     $className = explode('_', $className);
     $className = array_map('ucfirst', $className);
     $path = $className;
     unset($path[count($path) - 1]);
     $path = implode(DIRECTORY_SEPARATOR, $path);
     $filename = $className[count($className) - 1] . '.php';
     $className = implode('_', $className);
     $filepath = $path_prefix . DIRECTORY_SEPARATOR . $path;
     $generator = new Zend_CodeGenerator_Php_Class();
     $generator->setName(self::$_classPrefix . $className)->setExtendedClass(self::$_extendedClass)->setProperty(array('name' => '_name', 'visibility' => 'protected', 'defaultValue' => $tableName));
     Z_Fs::create_file($filepath . DIRECTORY_SEPARATOR . $filename, "<?\n" . $generator->generate());
 }