phpdev
phpdev

Reputation: 511

How to migrate and insert values in one class and migration in yii 1

I have following code in my migration file:

public function up() {
    $this->execute('
        CREATE TABLE IF NOT EXISTS `ad_court` (
        `id` int(11) NOT NULL AUTO_INCREMENT,
        `name` varchar(255) NOT NULL,
        PRIMARY KEY (`id`)
    ) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=18 ;');

    $this->insert('ad_court',array(
        'name' =>'Хўжалик суди',
    ));
    $this->insert('ad_court',array(
        'name' =>'Фуқаролик суди',
    ));
}

But, when I migrate it is showing error message:

 insert into ad_court ...exception 'CDbException' with message
      'CDbCommand failed to execute the SQL
 statement: SQLSTATE[42S02]: Base table or view not found: 
 1146 Table 'chamber_local_site.ad_court' doesn't exist.

I need to create a new table and insert values inside it. How can I do it in one migration class?

Upvotes: 1

Views: 2190

Answers (1)

dimis283
dimis283

Reputation: 222

As user guide there is that code,you may use this:

class m101129_185401_create_news_table extends CDbMigration {
    public function up() {
        $this -> createTable('tbl_news', array(
                'id' => 'pk',
                'title' => 'string NOT NULL',
                'content' => 'text',
            ));
    }

    public function down() {
        $this -> dropTable('tbl_news');
    }
}

Upvotes: 1

Related Questions