3

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;
Kevin
  • 4,823
  • 6
  • 36
  • 70

3 Answers3

2

The Sluggable extension works on both create and update actions. Therefore, you could just simulate an update by putting a row's name with its own.

systemasis
  • 135
  • 10
  • From the documentation "updatable (optional, default=true) - true to update the slug on sluggable field changes, false - otherwise". So if the name does not change, it won't be redefined. – Alsatian Jul 26 '16 at 16:08
  • As I understand, Sluggable doesn't compare the two versions of a slugged field but instead is triggered when that field is changed. Meaning updating an entity with the same values is still an update. – systemasis Aug 19 '16 at 09:56
2

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);
}
Kevin
  • 4,823
  • 6
  • 36
  • 70
  • That was my answer ... But you have to make your slug url friendly => preg_replace, and to make it unique => query – Alsatian Jul 26 '16 at 14:37
  • But the Doctrine Sluggable extension is already taking care of that, right? – Kevin Jul 26 '16 at 15:57
  • You are right for the uniqueness, from the documentation : "sometimes you might need to set it manually, etc if generated one does not look satisfying enough. Sluggable will ensure uniqueness of the slug." So it will check for uniqueness, but they won't be generated like in the extension (lower, without space ...) – Alsatian Jul 26 '16 at 16:13
0

Just saw that the Gedmo library documentation directly answers this question :

Regenerating slug

In case if you want the slug to regenerate itself based on sluggable fields, set the slug to null.

<?php $entity = $em->find('Entity\Something', $id);
$entity->setSlug(null);

$em->persist($entity); $em->flush();

So, in your case you have just to persist the entity, nothing else. Because $slug is already null.

Alsatian
  • 3,086
  • 4
  • 25
  • 40