예제 #1
0
파일: CLIMenu.php 프로젝트: o-log/php-model
 public static function run()
 {
     while (true) {
         echo CliUtil::delimiter();
         echo "Choose PHPModel function:\n";
         echo "\t" . self::FUNCTION_EXECUTE_SQL . ": execute new SQL queries from registry\n";
         echo "\t" . self::FUNCTION_CREATE_MODEL . ": create model\n";
         echo "\t" . self::FUNCTION_ADD_MODEL_FIELD . ": add field to existing model\n";
         echo "\t" . self::FUNCTION_MODEL_FIELD_EXTRAS . ": extra operations on model field\n";
         echo "\tENTER: exit\n";
         $command_str = CliUtil::readStdinAnswer();
         switch ($command_str) {
             case self::FUNCTION_EXECUTE_SQL:
                 CLIExecuteSql::executeSqlScreen();
                 break;
             case self::FUNCTION_CREATE_MODEL:
                 CLICreateModel::enterClassNameScreen();
                 break;
             case self::FUNCTION_ADD_MODEL_FIELD:
                 $cli_add_field_obj = new CLIAddFieldToModel();
                 $cli_add_field_obj->addField();
                 break;
             case self::FUNCTION_MODEL_FIELD_EXTRAS:
                 $cli_add_field_obj = new CLIAddFieldToModel();
                 $cli_add_field_obj->extraFieldFunctionsScreen();
                 break;
             default:
                 exit;
         }
     }
 }
예제 #2
0
 public static function generateClass()
 {
     //
     $cwd = getcwd();
     // TODO: model_namespace_for_path may have leading '/' - remove it?
     $model_filename = $cwd . DIRECTORY_SEPARATOR . self::$model_namespace_for_path . DIRECTORY_SEPARATOR . self::$model_class_name . '.php';
     $model_tablename = mb_strtolower(self::$model_namespace_for_class . "\\" . self::$model_class_name);
     $model_tablename = preg_replace('@\\W@', '_', $model_tablename);
     //
     // creating model class file
     //
     $class_file = self::getClassTemplate();
     // TODO: use common variable replacemnt method
     $class_file = str_replace('TEMPLATECLASS_CLASSNAME', self::$model_class_name, $class_file);
     $class_file = str_replace('TEMPLATECLASS_NAMESPACE', self::$model_namespace_for_class, $class_file);
     $class_file = str_replace('TEMPLATECLASS_TABLENAME', $model_tablename, $class_file);
     $class_file = str_replace('TEMPLATECLASS_DBID', self::$model_db_id, $class_file);
     self::file_force_contents($model_filename, $class_file);
     echo "\nModel file created: " . $model_filename . "\n";
     //
     // altering database sql file
     //
     $class_sql = self::getClassSQL();
     // TODO: use common variable replacemnt method
     $class_sql = str_replace('TEMPLATECLASS_TABLENAME', $model_tablename, $class_sql);
     CLIExecuteSql::addSqlToRegistry(self::$model_db_id, $class_sql);
     echo "\nSQL registry updated\n";
     echo "\nType ENTER to execute SQL queries, Ctrl+C to exit.\n";
     $command_str = CliUtil::readStdinAnswer();
     if ($command_str == '') {
         CLIExecuteSql::executeSqlScreen();
     }
     return;
 }
예제 #3
0
 public function addForeignKey()
 {
     Assert::assert($this->field_name);
     Assert::assert($this->model_file_path);
     $model_db_id = $this->getDbIdFromClassFile();
     $model_table_name = $this->getTableNameFromClassFile();
     Assert::assert($model_table_name);
     Assert::assert($model_db_id);
     // TODO: select model instead of table name?
     echo "Enter target db table name:\n";
     $target_table_name = trim(fgets(STDIN));
     // TODO: check table name format
     // TODO: select from target model fields?
     echo "Enter target db table field name:\n";
     $target_field_name = trim(fgets(STDIN));
     // TODO: check field name format
     $on_delete_action = '';
     echo "Choose foreign key action on delete:\n";
     echo "\tc: cascade (delete this model when deleting referenced model)\n";
     echo "\tENTER: default action (restrict deleting referenced model)\n";
     $delete_action_code = trim(fgets(STDIN));
     if ($delete_action_code == 'c') {
         $on_delete_action = ' on delete cascade ';
     }
     $sql = 'alter table ' . $model_table_name . ' add constraint FK_' . $this->field_name . '_' . rand(0, 999999) . ' foreign key (' . $this->field_name . ')  references ' . $target_table_name . ' (' . $target_field_name . ') ' . $on_delete_action . '/* rand' . rand(0, 999999) . ' */;';
     CLIExecuteSql::addSqlToRegistry($model_db_id, $sql);
     echo "\nSQL registry updated\n";
 }