/** * Update a config in the database. * @param string $key The config element to update (or create) * @param string $value The value to put * @return string The old value from this config */ public function set($key, $value) { $element = new Config(); $element->setName($key); try { /** @var Config $element */ $element = $this->createQueryBuilder('c')->where('c.name = ?1')->setParameter(1, $key)->getQuery()->getSingleResult(); } catch (NoResultException $e) { // Just ignore, we will create a new element } $oldValue = $element->getValue(); $element->setValue($value); $this->getEntityManager()->persist($element); $this->getEntityManager()->flush($element); return $oldValue; }
/** * {@inheritdoc} */ protected function execute(InputInterface $input, OutputInterface $output) { $em = $this->getContainer()->get("doctrine.orm.entity_manager"); $em->beginTransaction(); $output->writeln("Clean the config ..."); $em->createQueryBuilder()->delete()->from('BdEMainBundle:Config', 'c')->getQuery()->execute(); $output->writeln("Config cleaned, now load the new config ..."); $kernel = $this->getContainer()->get('kernel'); $path = $kernel->locateResource("@BdEMainBundle/Resources/config/default_config.yml"); $defaultConfig = Yaml::parse(file_get_contents($path)); foreach ($defaultConfig as $name => $value) { $config = new Config(); $config->setName($name); $config->setValue($value); $em->persist($config); $output->writeln("Create " . $name . " with value " . $value); } $em->flush(); $em->commit(); $output->writeln("Done", OutputInterface::OUTPUT_NORMAL); }