Created
October 17, 2012 06:57
-
-
Save giorrrgio/3904104 to your computer and use it in GitHub Desktop.
Symfony 2.1 Doctrine Migrations and Sluggable
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Application\Migrations; | |
use Symfony\Bundle\FrameworkBundle\Console\Application; | |
use Acme\MyBundle\Command\UpdateSlugsCommand; | |
use Symfony\Component\Console\Tester\CommandTester; | |
use Doctrine\DBAL\Migrations\AbstractMigration, | |
Doctrine\DBAL\Schema\Schema; | |
/** | |
* Auto-generated Migration: Please modify to your need! | |
*/ | |
class Version20120918201002 extends AbstractMigration | |
{ | |
protected static $class = 'AppKernel'; | |
protected static $kernel; | |
/** | |
* Creates a Kernel. | |
* | |
* Available options: | |
* | |
* * environment | |
* * debug | |
* | |
* @param array $options An array of options | |
* | |
* @return HttpKernelInterface A HttpKernelInterface instance | |
*/ | |
protected static function createKernel(array $options = array()) | |
{ | |
if (null === static::$class) { | |
static::$class = static::getKernelClass(); | |
} | |
return new static::$class( | |
isset($options['environment']) ? $options['environment'] : 'test', | |
isset($options['debug']) ? $options['debug'] : true | |
); | |
} | |
/** | |
* Creates a Client. | |
* | |
* @param array $options An array of options to pass to the createKernel class | |
* @param array $server An array of server parameters | |
* | |
* @return Client A Client instance | |
*/ | |
protected static function createClient(array $options = array(), array $server = array()) | |
{ | |
if (null !== static::$kernel) { | |
static::$kernel->shutdown(); | |
} | |
static::$kernel = static::createKernel($options); | |
static::$kernel->boot(); | |
$client = static::$kernel->getContainer()->get('test.client'); | |
$client->setServerParameters($server); | |
return $client; | |
} | |
public function up(Schema $schema) | |
{ | |
// this up() migration is autogenerated, please modify it to your needs | |
$this->abortIf($this->connection->getDatabasePlatform()->getName() != "mysql"); | |
$this->addSql("ALTER TABLE foobar ADD slug VARCHAR(128) DEFAULT NULL"); | |
$this->addSql("CREATE UNIQUE INDEX UNIQ_98197A65989D9B62 ON foobar (slug)"); | |
} | |
public function postUp(Schema $schema) | |
{ | |
$this->client = self::createClient(); | |
$this->application = new Application($this->client->getKernel()); | |
$this->application->add(new SetupUpdateSlugsCommand()); | |
$this->em = $this->client->getKernel()->getContainer()->get('doctrine')->getEntityManager(); | |
$command = $this->application->find('acme:update-slugs'); | |
$commandTester = new CommandTester($command); | |
$output = $commandTester->execute(array('command' => $command->getName(), 'class' => 'AcmeMyBundle:Foobar')); | |
echo $output; | |
} | |
public function down(Schema $schema) | |
{ | |
// this down() migration is autogenerated, please modify it to your needs | |
$this->abortIf($this->connection->getDatabasePlatform()->getName() != "mysql"); | |
$this->addSql("DROP INDEX UNIQ_98197A65989D9B62 ON foobar"); | |
$this->addSql("ALTER TABLE foobar DROP slug"); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Application\Migrations; | |
use Doctrine\DBAL\Migrations\AbstractMigration, | |
Doctrine\DBAL\Schema\Schema; | |
/** | |
* Auto-generated Migration: Please modify to your need! | |
*/ | |
class Version20120918203846 extends AbstractMigration | |
{ | |
public function up(Schema $schema) | |
{ | |
$this->addSql("ALTER TABLE foobar CHANGE slug slug VARCHAR( 128 ) NOT NULL "); | |
} | |
public function down(Schema $schema) | |
{ | |
$this->addSql("ALTER TABLE foobar CHANGE slug slug VARCHAR(128) DEFAULT NULL"); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
acme.slugs_updater: | |
class: Acme\MyBundle\Service\SlugsUpdater | |
arguments: ["@doctrine.orm.entity_manager"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Acme\MyBundle\Command; | |
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; | |
use Symfony\Component\Console\Input\InputArgument; | |
use Symfony\Component\Console\Input\InputInterface; | |
use Symfony\Component\Console\Input\InputOption; | |
use Symfony\Component\Console\Output\OutputInterface; | |
use Symfony\Component\Console\Formatter\OutputFormatterStyle; | |
class SetupUpdateSlugsCommand extends ContainerAwareCommand | |
{ | |
protected function configure() | |
{ | |
$this | |
->setName('acme:update-slugs') | |
->setDescription('Update slugs for a given entity') | |
->addArgument('class', null, InputArgument::REQUIRED, 'Specify target class') | |
; | |
} | |
protected function execute(InputInterface $input, OutputInterface $output) | |
{ | |
$style = new OutputFormatterStyle('white', 'red', array('bold', 'blink')); | |
$output->getFormatter()->setStyle('error', $style); | |
$dic = $this->getContainer(); | |
$updater = $dic->get('acme.slugs_updater'); | |
$updater->update($input->getArgument('class')); | |
$output->writeln('done.'); | |
return; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Acme\MyBundle\Service; | |
use Doctrine\ORM\EntityManager; | |
class SlugsUpdater | |
{ | |
protected $em; | |
public function __construct(EntityManager $em) | |
{ | |
$this->em = $em; | |
} | |
public function update($class) | |
{ | |
$repo = $this->em->getRepository($class); | |
$entities = $repo->findAll(); //you should be more restrictive here | |
foreach ($entities as $entity) { | |
$entity->setSlug(''); | |
$this->em->persist($entity); | |
$this->em->flush(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment