Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to use model in migration in Yii 1.x

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?

like image 729
zonky Avatar asked Oct 10 '11 10:10

zonky


Video Answer


1 Answers

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.

like image 184
deepwell Avatar answered Oct 04 '22 08:10

deepwell