这是我的实体,我使用了gedmo注释。当创建新的注册信息(持续存在)时,slug正常工作,但如何从现有数据库自动生成slug文本呢?
/**
* @Gedmo\Slug(fields={"name"})
* @ORM\Column(type="string", unique=true)
*/
protected $slug;
这是我的实体,我使用了gedmo注释。当创建新的注册信息(持续存在)时,slug正常工作,但如何从现有数据库自动生成slug文本呢?
/**
* @Gedmo\Slug(fields={"name"})
* @ORM\Column(type="string", unique=true)
*/
protected $slug;
<?php
namespace App\Command;
use App\Entity\Foo;
use App\Entity\Bar;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class RegenerateSlugs extends Command
{
private $doctrine;
protected static $defaultName = "app:regenerate-slugs";
public function __construct(ManagerRegistry $doctrine)
{
parent::__construct();
$this->doctrine = $doctrine;
}
protected function configure(): void
{
$this
->setDescription('Regenerate the slugs for all Foo and Bar entities.')
;
}
protected function execute(InputInterface $input, OutputInterface $output): void
{
$manager = $this->doctrine->getManager();
// Change the next line by your classes
foreach ([Foo::class, Bar::class] as $class) {
foreach ($manager->getRepository($class)->findAll() as $entity) {
$entity->setSlug(null);
//$entity->slug = null; // If you use public properties
}
$manager->flush();
$manager->clear();
$output->writeln("Slugs of \"$class\" updated.");
}
}
}
嗨,希望这可以帮助到那些遇到这个问题的人!