I had to add in an existing entity a slug field to slugify the field 'name'. But there is already data in this entity and I can't delete them.
I would like to create a console script which can slugify all my 'name' field.
I don't know how to do it because this is not an insertion but just an update...
class SlugCommand extends ContainerAwareCommand
{
protected function configure()
{
$this
->setName('generate:geo:slug')
->setDescription('Slug generation for GeoBundle ');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$em = $this->getContainer()->get('doctrine')->getManager();
$regions = $em->getRepository('FMGeoBundle:Region')->findAll();
if($regions === null){
throw new Exception('No Region found');
}
foreach($regions as $region){
// ????? Generate the slug here ??
$em->persist($region);
}
$em->flush();
$output->writeln('Slugs Generated ;) ...');
}
}
The 'slug' field in my entity:
/**
* @var string
*
* @ORM\Column(name="slug", type="string", length=255)
* @Gedmo\Slug(fields={"name"})
*/
protected $slug;
I found an easier way. You can apparently just set the slug manually like that. And it will slugify the field needed.
foreach ($regions as $region) {
$region->setSlug($region->getName());
$this->em->persist($region);
}