2013-10-21 15 views
15

Mam ten kod, ale nie działa:Zastosowanie EntityManager w migracjach złożyć

<?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"); 
    } 
} 

muszę dodać informacje na telefon w nowej tellphone pola.

Dzięki

+0

Co masz na myśli z 'nie działa'. Jak nazywa się te metody? Jaki jest przewidywany i aktualny wynik? – ferdynator

+0

Ta linia otrzymuje błędy $ em = $ em = $ this-> getDoctrine() -> getEntityManager(); Potrzebuję użyć EntityManager w tym pliku. Dzięki – Zarpele

+0

Nie działa, ponieważ wywołujesz $ this-> getDoctrine(), klasa AbstractMigration nie ma tej metody. – zuzuleinen

Odpowiedz

36

Może to być starszy wpis, ale tymczasem problem został rozwiązany i stanowi część aktualnej dokumentacji.

Zobacz http://symfony.com/doc/current/bundles/DoctrineMigrationsBundle/index.html#container-aware-migrations

// ... 
use Symfony\Component\DependencyInjection\ContainerAwareInterface; 
use Symfony\Component\DependencyInjection\ContainerInterface; 
use Symfony\Component\DependencyInjection\ContainerAwareTrait; 

class Version20130326212938 extends AbstractMigration implements ContainerAwareInterface 
{ 
    use ContainerAwareTrait; 

    public function up(Schema $schema) 
    { 
     // ... migration content 
    } 

    public function postUp(Schema $schema) 
    { 
     $em = $this->container->get('doctrine.orm.entity_manager'); 
     // ... update the entities 
    } 
} 
5

muszą zgłosić się do zmian w sposobie postUp() - te addSql() - Oświadczenia będą realizowane po up() metoda jest zakończona, dzięki czemu nowe wiersze (tj tellphone) nie są dostępne w metodzie up()!