Skip to content

Instantly share code, notes, and snippets.

@Khodl
Last active October 4, 2016 23:36
Show Gist options
  • Save Khodl/5cf8fad1735ad963133a62cc71ce777d to your computer and use it in GitHub Desktop.
Save Khodl/5cf8fad1735ad963133a62cc71ce777d to your computer and use it in GitHub Desktop.
Populate or update your data with one Symfony command
<?php
/*
* By Khodl.me
* Gist: https://gist.github.com/Khodl/5cf8fad1735ad963133a62cc71ce777d
*/
namespace AppBundle\Command;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class PopulateCommand extends ContainerAwareCommand
{
private $fields = [
'Location' => 'slug',
'Thing' => 'slug'
];
protected function configure()
{
$this
->setName('app:populate')
->setDescription('Will add or edit entities in the database')
->setHelp("Edit AppBundle/Command/PopulateCommand.php")
;
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$em = $this->getContainer()->get('doctrine')->getManager();
foreach($this->fields AS $entityName => $id){
$output->writeln("");
$output->writeln('<info>Loading data for '.$entityName.':</info>');
$getId = $id;
$getId{0} = strtoupper($getId{0});
$getId = 'get'.$getId;
$repository = $em->getRepository('AppBundle:'.$entityName);
$method = $entityName.'Data';
$class = 'AppBundle\\Entity\\'.$entityName;
// Prepare data that should be there
$d = [];
foreach($this->$method() as $data){
$d[$data[$id]] = $data;
}
// Update existing data (based on identifier)
foreach($repository->findAll() as $entity){
$id = $entity->$getId();
if(isset($d[$id])){
$output->writeln("* Updating $entityName '$id'.");
$this->updateData($entity,$d[$id]);
unset($d[$id]);
}else{
$output->writeln("<comment>* WARN: $entityName '$id' in database but not in $method().</comment>");
}
}
// Adding the ones that don't exist yet
foreach($d as $id => $data){
$entity = new $class();
$output->writeln("* Creating new $entityName '$id'.");
$this->updateData($entity,$data);
$em->persist($entity);
}
$output->writeln("");
}
$em->flush();
}
private function updateData($entity,$data){
foreach($data as $attribute => $value){
$attribute{0} = strtoupper($attribute{0});
$entity->{'set'.$attribute}($value);
}
return $entity;
}
protected function LocationData(){
return [
[
'slug' => 'hall',
'tileImage' => 'hall',
'service' => 'hall',
'illustration' => 'hall',
'accessible' => true,
],[
'slug' => 'cell',
'tileImage' => 'cell',
'service' => 'cell',
'illustration' => 'cell',
'accessible' => true,
],[
'slug' => 'kitchen',
'tileImage' => 'kitchen',
'service' => 'kitchen',
'illustration' => 'kitchen',
'accessible' => true,
],[
'slug' => 'infirmary',
'tileImage' => 'infirmary',
'service' => 'infirmary',
'illustration' => 'infirmary',
'accessible' => true,
],[
'slug' => 'guards',
'tileImage' => 'guards',
'service' => 'guards',
'illustration' => 'guards',
'accessible' => true,
]
];
}
function ThingData(){
return [
[
'slug' => 'tissue',
]
];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment