how to seed in Yii?

醉酒当歌 提交于 2019-12-22 09:06:48

问题


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;
}

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();
}

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

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.


回答1:


Use

public function safeUp()
{
   $this->insert('users',array(
      'login'=>'Bob'));
}

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



来源:https://stackoverflow.com/questions/17114708/how-to-seed-in-yii

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!