5

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.

Andrew
  • 2,148
  • 5
  • 23
  • 34

1 Answers1

7

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

jmarkmurphy
  • 11,030
  • 31
  • 59
  • Thanks a lot! Once the answer is given, it seems logic. I must have overlooked this approach. – Andrew Jun 14 '13 at 19:47