symfony - Symfony 3 修改 slug gedmo 注释
问题描述
我的实体中有一个字段:
/**
* slug field
*
* @Gedmo\Slug(fields={"name"})
* @ORM\Column(type="string", length=255, unique=true)
*
* @var string
*/
private $slug;
现在我想修改这个字段以从两个字段中生成 slug。
/**
* slug field
*
* @Gedmo\Slug(fields={"id", "name"})
* @ORM\Column(type="string", length=255, unique=true)
*
* @var string
*/
private $slug;
但是当我保存这个更改时,它仍然只从字段“名称”中产生 slug。如何保存此注释更改?
解决方案
您是否尝试过它是否适用于新实体?更改配置后,旧的 slug 不会自动更新。您必须取消设置所有现有实体的 slug 并再次保存它们以生成新的 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();
}
}
推荐阅读
- stock - 如何处理股票数据中的缺失柱?
- c++ - 使用 str += "A" 或 str = str + "A" 的字符串 concat 之间的性能差异
- simulation - ANYLOGIC - 在特定间隔的日志中剪切数据
- python - Python TigerGraph GSQL的问题-多行字符串中的EOF
- android - 如何获取 Firebase 的系统时间戳?在科特林
- angular - 角材料 - 类型“字符串”不可分配给类型“MatSnackBarHorizontalPosition”
- calculation - CRC32 是 32 位的吗?
- javascript - Shopify 将 JS 转换为 AMP 代码格式的建议
- javascript - 将连接元素数组转换为树
- scripting - 如何解决热键冲突?