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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With