I have this code but does not work:
<?php
namespace Application\Migrations;
use Doctrine\DBAL\Migrations\AbstractMigration,
Doctrine\DBAL\Schema\Schema;
/**
* Auto-generated Migration: Please modify to your need!
*/
class Version20131021150555 extends AbstractMigration
{
public function up(Schema $schema)
{
// this up() migration is auto-generated, please modify it to your needs
$this->abortIf($this->connection->getDatabasePlatform()->getName() != "mysql", "Migration can only be executed safely on 'mysql'.");
$this->addSql("ALTER TABLE person ADD tellphone LONGTEXT DEFAULT NULL");
$em = $em = $this->getDoctrine()->getEntityManager();
$persons = $em->getRepository('AutogestionBundle:Person')->fetchAll();
foreach($persons as $person){
$person->setTellPhone($person->getCellPhone());
$em->persist($person);
}
$em->flush();
}
public function down(Schema $schema)
{
// this down() migration is auto-generated, please modify it to your needs
$this->abortIf($this->connection->getDatabasePlatform()->getName() != "mysql", "Migration can only be executed safely on 'mysql'.");
$this->addSql("ALTER TABLE person DROP tellphone");
}
}
I have add info in cellphone in a new field tellphone.
Thanks
You must call your modifications in the
postUp()
method - theaddSql()
- Statements will be executed afterup()
method is completed, so your new rows (i.e. tellphone) are not available during theup()
method!This may be an older post, but meanwhile the problem is solved and actually part of the current documentation.
See http://symfony.com/doc/current/bundles/DoctrineMigrationsBundle/index.html#container-aware-migrations