Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to seed in Yii?

Tags:

migrate

seed

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

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.

like image 640
Andrew Avatar asked Jun 14 '13 18:06

Andrew


1 Answers

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

like image 59
jmarkmurphy Avatar answered Dec 23 '22 15:12

jmarkmurphy