Artem Khodos
Artem Khodos

Reputation: 192

Doctrine 1 Migrations, How to execute raw SQL

My migration logic includes pretty complex actions like inserting data to just created tables and Stored Procedures creation. If there any way to create/generate proper migration on Symfony 1 using Doctrine 1? The fastest way in my opinion is to create migration with RAW SQL commands I need.

Upvotes: 1

Views: 2178

Answers (2)

Kris Peeling
Kris Peeling

Reputation: 1025

What I do in these cases is create an empty migration class using the symfony doctrine:generate-migration command and then fill in both up() and down() methods similar to this:

public function up()
{
    $dbh = Doctrine_Manager::connection()->getDbh();
    $query = "INSERT INTO `some_table` (`id`, `created_at`, `updated_at`)
              VALUES 
              ('1', NOW(), NOW()), 
              ('2', NOW(), NOW()),
              ('3', NOW(), NOW());
    ";
    $stmt = $dbh->prepare($query);
    $stmt->execute();
}

Upvotes: 2

Alessandro Minoccheri
Alessandro Minoccheri

Reputation: 35963

You can try this:

$connection = Doctrine_Manager::getInstance()->getCurrentConnection();
$results = $connection->execute(" -- YOUR RAW SQL -- ");

Upvotes: 0

Related Questions