Просмотр файла database/upgrades/20170312112433_change_user_in_photo.php

Размер файла: 1.4Kb
<?php

use Phinx\Migration\AbstractMigration;

class ChangeUserInPhoto extends AbstractMigration
{
    /**
     * Migrate Up.
     */
    public function up()
    {
        $table = $this->table('photo');

        $rows = $this->fetchAll('SELECT * FROM photo');
        foreach($rows as $row) {

            $user = 0;
            if (!empty($row['user'])) {
                $user = $this->fetchRow('SELECT id FROM users WHERE login = "'.$row['user'].'" LIMIT 1;');
            }

            $userId = ! empty($user) ? $user['id'] : 0;

            $this->execute('UPDATE photo SET user="'.$userId.'" WHERE id = "'.$row['id'].'" LIMIT 1;');
        }

        $table
            ->changeColumn('user', 'integer')
            ->save();

        $table->renameColumn('user', 'user_id');
        $table->renameColumn('time', 'created_at');

        $table
            ->removeIndexByName('time')
            ->removeIndexByName('user')
            ->addIndex('created_at')
            ->addIndex('user_id')
            ->save();
    }

    /**
     * Migrate Down.
     */
    public function down()
    {
        $table = $this->table('photo');
        $table
            ->renameColumn('user_id', 'user')
            ->renameColumn('created_at', 'time')
            ->removeIndexByName('created_at')
            ->removeIndexByName('user_id')
            ->addIndex('time')
            ->addIndex('user')
            ->save();

    }
}