6

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?

trejder
  • 17,148
  • 27
  • 124
  • 216
zonky
  • 1,058
  • 9
  • 11
  • This may seem like an obvious question but... do you have the proper CACtiveRecord model set up, does it specify the correct `tableName()`, and does the table exist in the database? Because I just did a quick test looping through some models in a migration it seemed to work fine for me. – thaddeusmt Oct 10 '11 at 11:58
  • @thaddeusmt: tableName, Models and Tables were right. but i never realized that in config/console.php is another db-configuration. after i changed that - it worked! thanks.. – zonky Oct 10 '11 at 15:14
  • Ah! Yeah, that's a tricky one. Glad you got it figured out! I have a "base" config file I use for the DB info, and then I use CMap::mergeArray to combine it with my web and console config files so they inherit the same DB info. – thaddeusmt Oct 11 '11 at 13:26

1 Answers1

5

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.

deepwell
  • 20,195
  • 10
  • 33
  • 39