开发者

Unable to use model in migration in Yii 1.x

开发者 https://www.devze.com 2023-04-12 10:58 出处:网络
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:

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 strin开发者_JS百科gs with SQL commands?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号