0

在尝试使用 Yii 应用数据库迁移时,我遇到了一个新问题。

首先,我使用这个 :: ./yiic migrate create tr 创建一个新的迁移

这给了我以下输出。

Yii 迁移工具 v1.0 (基于 Yii v1.1.14)

创建新的迁移“/var/www/html/Trackstar/protected/migrations/m130921_101251_tr.php”?(yes|no) [no]:y 新迁移成功创建。

这表明我已经创建了一个新的迁移。

现在我在 up() 和 down() 中添加了必要的 ddl。这是它的外观。

<?php

class m130921_101251_tr extends CDbMigration {

    public function up() {

        //create the issue table
        $this->createTable('tbl_issue', array(
            'id' => 'pk',
            'name' => 'string NOT NULL',
            'description' => 'text',
            'project_id' => 'int(11) DEFAULT NULL',
            'type_id' => 'int(11) DEFAULT NULL',
            'status_id' => 'int(11) DEFAULT NULL',
            'owner_id' => 'int(11) DEFAULT NULL',
            'requester_id' => 'int(11) DEFAULT NULL',
            'create_time' => 'datetime DEFAULT NULL',
            'create_user_id' => 'int(11) DEFAULT NULL',
            'update_time' => 'datetime DEFAULT NULL',
            'update_user_id' => 'int(11) DEFAULT NULL',
                ), 'ENGINE=InnoDB');
//create the user table
        $this->createTable('tbl_user', array(
            'id' => 'pk',
            'username' => 'string NOT NULL',
            'email' => 'string NOT NULL',
            'password' => 'string NOT NULL',
            'last_login_time' => 'datetime DEFAULT NULL',
            'create_time' => 'datetime DEFAULT NULL',
            'create_user_id' => 'int(11) DEFAULT NULL',
            'update_time' => 'datetime DEFAULT NULL',
            'update_user_id' => 'int(11) DEFAULT NULL',
                ), 'ENGINE=InnoDB');



        //create the assignment table that allows for many-to-many
//relationship between projects and users
        $this->createTable('tbl_project_user_assignment', array(
            'project_id' => 'int(11) NOT NULL',
            'user_id' => 'int(11) NOT NULL',
            'PRIMARY KEY (`project_id`,`user_id`)',
                ), 'ENGINE=InnoDB');
//foreign key relationships
//the tbl_issue.project_id is a reference to tbl_project.id
        $this->addForeignKey("fk_issue_project", "tbl_issue", "project_id", "tbl_project", "id", "CASCADE", "RESTRICT");
//the tbl_issue.owner_id is a reference to tbl_user.id
        $this->addForeignKey("fk_issue_owner", "tbl_issue", "owner_id", "tbl_user", "id", "CASCADE", "RESTRICT");
//the tbl_issue.requester_id is a reference to tbl_user.id
        $this->addForeignKey("fk_issue_requester", "tbl_issue", "requester_id", "tbl_user", "id", "CASCADE", "RESTRICT");
//the tbl_project_user_assignment.project_id is a reference to        tbl_project.id
        $this->addForeignKey("fk_project_user", "tbl_project_user_assignment", "project_id", "tbl_project", "id", "CASCADE", "RESTRICT");
//the tbl_project_user_assignment.user_id is a reference to tbl_        user.id
        $this->addForeignKey("fk_user_project", "tbl_project_user_assignment", "user_id", "tbl_user", "id", "CASCADE", "RESTRICT");
    }

    public function down() {
        $this->truncateTable('tbl_project_user_assignment');
        $this->truncateTable('tbl_issue');
        $this->truncateTable('tbl_user');
        $this->dropTable('tbl_project_user_assignment');
        $this->dropTable('tbl_issue');
        $this->dropTable('tbl_user');
    }

    /*
      // Use safeUp/safeDown to do migration with transaction
      public function safeUp()
      {
      }

      public function safeDown()
      {
      }
     */
}

现在的问题是,每当我尝试使用 ./yiic migrate 应用此迁移时,它都会失败并出现相同的错误。这是我得到的示例输出。

[root@localhost protected]# ./yiic migrate

Yii Migration Tool v1.0 (based on Yii v1.1.14)

Total 1 new migration to be applied:
    m130921_101251_tr

Apply the above migration? (yes|no) [no]:y
*** applying m130921_101251_tr
PHP Error[2]: include(m130921_101251_tr.php): failed to open stream: No such file or directory
    in file /var/www/html/yii/framework/YiiBase.php at line 427
#0 /var/www/html/yii/framework/YiiBase.php(427): autoload()
#1 unknown(0): autoload()
#2 /var/www/html/yii/framework/cli/commands/MigrateCommand.php(429): spl_autoload_call()
#3 /var/www/html/yii/framework/cli/commands/MigrateCommand.php(384): MigrateCommand->instantiateMigration()
#4 /var/www/html/yii/framework/cli/commands/MigrateCommand.php(109): MigrateCommand->migrateUp()
#5 unknown(0): MigrateCommand->actionUp()
#6 /var/www/html/yii/framework/console/CConsoleCommand.php(172): ReflectionMethod->invokeArgs()
#7 /var/www/html/yii/framework/console/CConsoleCommandRunner.php(71): MigrateCommand->run()
#8 /var/www/html/yii/framework/console/CConsoleApplication.php(92): CConsoleCommandRunner->run()
#9 /var/www/html/yii/framework/base/CApplication.php(180): CConsoleApplication->processRequest()
#10 /var/www/html/yii/framework/yiic.php(33): CConsoleApplication->run()
#11 /var/www/html/Trackstar/protected/yiic.php(7): require_once()
#12 /var/www/html/Trackstar/protected/yiic(4): require_once()

我似乎无法找到上述问题的解决方案。我一直在 google、stackoverflow 和 yii 论坛上搜索相关答案,但我没有找到。请提供有关如何解决此问题的任何帮助。我是 Yii 的新手,所以我还在学习,我很喜欢它。但被困在第一步是一个真正的挫折。任何形式的帮助将不胜感激。

谢谢,麦克斯

如需更多说明。我正在发布 config/console.php 和 config/main.php

-------------config/console.php--------------

<?php

// This is the configuration for yiic console application.
// Any writable CConsoleApplication properties can be configured here.
return array(
    'basePath'=>dirname(__FILE__).DIRECTORY_SEPARATOR.'..',
    'name'=>'My Console Application',

    // preloading 'log' component
    'preload'=>array('log'),

    // application components
    'components'=>array(

        // uncomment the following to use a MySQL database

        'db'=>array(
            'connectionString' => 'mysql:host=localhost;dbname=trackstar',
            'emulatePrepare' => true,
            'username' => 'user1',
            'password' => 'mydb389',
            'charset' => 'utf8',
        ),

        'log'=>array(
            'class'=>'CLogRouter',
            'routes'=>array(
                array(
                    'class'=>'CFileLogRoute',
                    'levels'=>'error, warning',
                ),
            ),
        ),
    ),
);

---------------------console.php 结束----------

---------------------config/main.php---------- -

<?php

// uncomment the following to define a path alias
// Yii::setPathOfAlias('local','path/to/local-folder');

// This is the main Web application configuration. Any writable
// CWebApplication properties can be configured here.
return array(
    'basePath'=>dirname(__FILE__).DIRECTORY_SEPARATOR.'..',
    'name'=>'My Web Application',

    // preloading 'log' component
    'preload'=>array('log'),

    // autoloading model and component classes
    'import'=>array(
        'application.models.*',
        'application.components.*',
    ),

    'modules'=>array(
        // uncomment the following to enable the Gii tool
        /*
        'gii'=>array(
            'class'=>'system.gii.GiiModule',
            'password'=>'Enter Your Password Here',
            // If removed, Gii defaults to localhost only. Edit carefully to taste.
            'ipFilters'=>array('127.0.0.1','::1'),
        ),
        */
    ),

    // application components
    'components'=>array(
        'user'=>array(
            // enable cookie-based authentication
            'allowAutoLogin'=>true,
        ),
        // uncomment the following to enable URLs in path-format
        /*
        'urlManager'=>array(
            'urlFormat'=>'path',
            'rules'=>array(
                '<controller:\w+>/<id:\d+>'=>'<controller>/view',
                '<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
                '<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
            ),
        ),
        */
        // uncomment the following to use a MySQL database
        'db'=>array(
            'connectionString' => 'mysql:host=localhost;dbname=trackstar',
            'emulatePrepare' => true,
            'username' => 'user1',
            'password' => 'mydb389',
            'charset' => 'utf8',
        ),
        'errorHandler'=>array(
            // use 'site/error' action to display errors
            'errorAction'=>'site/error',
        ),
        'log'=>array(
            'class'=>'CLogRouter',
            'routes'=>array(
                array(
                    'class'=>'CFileLogRoute',
                    'levels'=>'error, warning',
                ),
                // uncomment the following to show log messages on web pages
                /*
                array(
                    'class'=>'CWebLogRoute',
                ),
                */
            ),
        ),
    ),

    // application-level parameters that can be accessed
    // using Yii::app()->params['paramName']
    'params'=>array(
        // this is used in contact page
        'adminEmail'=>'webmaster@example.com',
    ),
);

----------------------------- config/main.php 结束-------------- --------

mysql> show tables
-> ;



+-------------------------+
| Tables_in_trackStar |
+-------------------------+
| tbl_migration           |
+-------------------------+
1 row in set (0.00 sec)

mysql> desc tbl_migration;
+------------+--------------+------+-----+---------+-------+
| Field      | Type         | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+-------+
| version    | varchar(255) | NO   | PRI | NULL    |       |
| apply_time | int(11)      | YES  |     | NULL    |       |
+------------+--------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
4

2 回答 2

3

编辑:您不应该重命名迁移文件类,因为文件名链接到数据库中的 tbl_migrations。因此,将您的迁移文件重命名为

<?php

class m130921_101251_tr extends CDbMigration { 

EDIT2:还要确保您的文件名是protected/migrations/m130921_101251_tr.php

EDIT3:下面是 Yii 迁移如何工作的逻辑流程,也许这会有所帮助


Yii Migration Tool v1.0 (based on Yii v1.1.14)

来到这一点意味着框架被包含在内并且您的 yiic.php 设置是正确的

Total 1 new migration to be applied:
    m130921_101251_tr

这几行意味着 yiic 能够扫描您的迁移目录并连接到您的数据库并检查迁移文件夹中的所有文件,并查看 tbl_migrations 中是否存在同名的行。没有行或列为新迁移的文件

Apply the above migration? (yes|no) [no]:y
   *** applying m130921_101251_tr

这行输出意味着 yiic 现在正在尝试应用迁移,为此它将尝试找到与CDbMigration框架类相同的类。如果 yiic 找不到这个类,你会得到这个错误。

PHP Error[2]: include(m130921_101251_tr.php): failed to open stream: No such file or directory in file /var/www/html/yii/framework/YiiBase.php at line 427

Yii 的工作方式总是使用 namspaces 查找与文件名相同的类名,这在 PHP 5.3 及更高版本中可用来自 Yii docs

命名空间类 命名空间类是指在非全局命名空间中声明的类。例如,application\components\GoogleMap 类在命名空间 application\components 中声明。使用命名空间类需要 PHP 5.3.0 或更高版本。

从版本 1.1.5 开始,可以使用命名空间类而不显式包含它。例如,我们可以创建一个新的 application\components\GoogleMap 实例,而无需显式包含相应的类文件。这可以通过增强的 Yii 类自动加载机制实现。

我能够模拟您的错误的唯一情况是不使用 Php 5.3 或更高版本或类名与文件不匹配

于 2013-09-21T04:54:30.313 回答
0

该文件未正确包含。

include(m130921_101251_tr.php);

这就是为什么它无法找到你的班级。尝试在您的构造

中为您的文件提供正确的路径。include

于 2013-09-21T04:52:52.450 回答