我的实体中有一个字段:
/**
* slug field
*
* @Gedmo\Slug(fields={"name"})
* @ORM\Column(type="string", length=255, unique=true)
*
* @var string
*/
private $slug;
现在,我想修改此字段以从两个字段中创建子弹。
/**
* slug field
*
* @Gedmo\Slug(fields={"id", "name"})
* @ORM\Column(type="string", length=255, unique=true)
*
* @var string
*/
private $slug;
但是,当我保存此更改时,它仍然只能从“名称”字段中生成。如何保存此注释更改?
答案 0 :(得分:0)
您是否尝试过将它用于新实体?更改配置后,旧的子弹不会自动更新。您必须取消设置所有现有实体的段,然后再次保存它们以生成新段。我用命令来做到这一点:
<?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();
}
}