无法在 Yii 1.x 中的迁移中使用模型
使用迁移来插入或更改表结构对我来说没有问题。但是,我在使用模型更改表内的数据时遇到问题。我的想法是做类似的事情:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
迁移绝对是一组 SQL 命令。我建议使用 SELECT REPLACE 命令。请参阅http://www.1keydata.com/sql/sql-replace.html
但是,只要包含所有必需的 php 文件,您所采用的方法也应该有效。您可能还需要基本模型:
并确保编辑 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:
And make sure you edit config/console.php to have the correct database information.