如何在Yii播种? [英] how to seed in Yii?
问题描述
我想知道一旦通过迁移创建表,该如何在Yii中播种? 我已经有了一个升级的方法:
I'm wondering how one can seed in Yii a table once it is created with migration? I've got a migration with an up-method:
public function up()
{
$this->createTable('users',array('id'=>"pk",
'login'=>'string NOT NULL'));
echo "table 'users' is created.\n";
return true;
}
我也有相应的 Users 用户模型及其 CRUD操作.当我尝试使用向上方法执行另一次迁移
I've got as well corresponding Users model and its CRUD actions. When I try to execute another migration with an up-method
public function up()
{
$user = new Users;
$user->login = "Bob";
return $user->save();
}
我收到以下错误: PHP错误[2]:include(users.php):无法打开流:没有这样的文件或目录 在文件MyYiiRoot \ yii \ framework \ YiiBase.php中的第421行
I get the following error: PHP Error[2]: include(users.php): failed to open stream: No such file or directory in file MyYiiRoot\yii\framework\YiiBase.php at line 421
我已经通过使用查询生成器(通过 insert 命令)成功达到了预期的结果,但我希望有更好的解决方法.
I've managed to achieve the desired result by using query builder (by means of insert command), but I hope there is a nicer way out.
推荐答案
使用
public function safeUp()
{
$this->insert('users',array(
'login'=>'Bob'));
}
您还可以执行更新,删除和许多其他操作.查看 http://www.yiiframework.com/doc/api/1.1/CDbMigration有关更多信息
You can also do update, delete and a host of other actions. Look at http://www.yiiframework.com/doc/api/1.1/CDbMigration for more information
这篇关于如何在Yii播种?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!