Edg*_*nso 5 slug symfony doctrine-orm
这是我的实体,我使用了 gedmo 注释,当创建新寄存器(保留)时,slug 可以正常工作,但是我如何从现有数据库自动生成 slug 文本
/**
* @Gedmo\Slug(fields={"name"})
* @ORM\Column(type="string", unique=true)
*/
protected $slug;
Run Code Online (Sandbox Code Playgroud)
您必须手动执行此操作,方法是选择所有不带 slug 的值并将 slug 值设置为 null,如 Sluggable 行为文档中所述。
https://github.com/Atlantic18/DoctrineExtensions/blob/master/doc/sluggable.md#regenerate-slug
这是一个简单的 Symfony 命令,用于重新生成给定类的所有 slugs:
<?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.");
}
}
}
Run Code Online (Sandbox Code Playgroud)
您好,希望它可以帮助遇到这个问题的人!
| 归档时间: |
|
| 查看次数: |
3324 次 |
| 最近记录: |