如何在 Yii 中播种? [英] how to seed in Yii?

查看:13
本文介绍了如何在 Yii 中播种?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道一旦通过迁移创建了表格,如何在 Yii 中播种?我有一个使用 up 方法的迁移:

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.
";
    return true;
}

我也有相应的用户模型及其CRUD操作.当我尝试使用 up-method 执行另一个迁移时

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):无法打开流:没有那个文件或目录在文件 MyYiiRootyiiframeworkYiiBase.php 第 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆