无法在 Yii 1.x 中的迁移中使用模型

发布于 2024-12-09 01:15:09 字数 941 浏览 0 评论 0原文

使用迁移来插入或更改表结构对我来说没有问题。但是,我在使用模型更改表内的数据时遇到问题。我的想法是做类似的事情:

public function up()
{
    $models = MyModel::model()->findAll();
    foreach ($models as $variable) {
        $variable->property = str_replace('.', ',', $variable->property);
        $variable->save();
    } 
}

看来我无法导入模型,因为我收到以下错误:

*** applying m111010_084827_convert_point_2_comma
PHP Error[2]: include(MyModel.php): failed to open stream: No such file or directory

如果我尝试之前导入模型:

$modelClass = Yii::import('application.models.*');

那么错误是:

*** applying m111010_084827_convert_point_2_comma
exception 'CDbException' with message 'The table "{{mymodel}}" for active record class "MyModel" cannot be found in the database.' in C:\...\yii\framework\db\ar\CActiveRecord.php:2276

问题出在哪里?我做错了什么?我应该如何以正确的方式在迁移中导入模型?或者也许我应该用 SQL 命令替换字符串?

Using migration to insert or change the table structure is no problem for me. But, I have a problems to to change the data inside a table using model. My idea is to do something like that:

public function up()
{
    $models = MyModel::model()->findAll();
    foreach ($models as $variable) {
        $variable->property = str_replace('.', ',', $variable->property);
        $variable->save();
    } 
}

It seems, that I'm unable to import the model, because I'm getting follwoing error:

*** applying m111010_084827_convert_point_2_comma
PHP Error[2]: include(MyModel.php): failed to open stream: No such file or directory

If I try to import the Model before:

$modelClass = Yii::import('application.models.*');

then the error is:

*** applying m111010_084827_convert_point_2_comma
exception 'CDbException' with message 'The table "{{mymodel}}" for active record class "MyModel" cannot be found in the database.' in C:\...\yii\framework\db\ar\CActiveRecord.php:2276

Where is the problem? What am I doingwrong? How should I import model in migration inthe right way? Or maybe I should replace the strings with SQL commands?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

丑疤怪 2024-12-16 01:15:09

迁移绝对是一组 SQL 命令。我建议使用 SELECT REPLACE 命令。请参阅http://www.1keydata.com/sql/sql-replace.html

但是,只要包含所有必需的 php 文件,您所采用的方法也应该有效。您可能还需要基本模型:

   Yii::import('application.models.*');
   Yii::import('application.models.base.*');

并确保编辑 config/console.php 以获取正确的数据库信息。

Migrations are definitely meant to be a set of SQL commands. I would recommend using a SELECT REPLACE command. See http://www.1keydata.com/sql/sql-replace.html

However, the approach you are taking should also work, as long as you include all the required php files. You might need your base models also:

   Yii::import('application.models.*');
   Yii::import('application.models.base.*');

And make sure you edit config/console.php to have the correct database information.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文