您是否尝试过它是否适用于新实体?更改配置后,旧的slug不会自动更新。您必须取消设置所有现有实体的段塞,然后再次保存它们以生成新段塞。我使用了一个命令:
<?php
namespace AppBundle\Command;
use AppBundle\Entity\YourEntityClass;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
/**
* Generates slugs again
*/
class UpdateSlugsCommand extends ContainerAwareCommand
{
protected function configure()
{
$this
->setName('appbundle:update-slugs')
->setDescription('generate new slugs')
->setHelp('needed after changing the config');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$em = $this->getContainer()->get('doctrine')->getEntityManager();
$entities = $em->getRepository(YourEntityClass::class)->findAll();
foreach ($entities as $entity) {
// unset slug to generate a new one
$entity->setSlug(null);
$em->persist($entity);
}
$em->flush();
}
}