2016-01-28 27 views
1

这是我的实体,而我使用的gedmo注解,一个新的寄存器被创建时(persits)蛞蝓正常工作,但我怎么能自动 - 从现有的数据库怎样才能蛞蝓领域从现有的数据在数据库 - 学说的Symfony2

/** 
* @Gedmo\Slug(fields={"name"}) 
* @ORM\Column(type="string", unique=true) 
*/ 
protected $slug; 
+0

要与这个蛞蝓“坚持”究竟是什么呢? – darkomen

+0

您需要更新'名称'字段 - 只需坚持实体不起作用。我认为gedmo特别倾听命名字段的更改。否则,你不得不写一个小功能来为你做。 –

+0

看看这个https://knpuniversity.com/screencast/symfony2-ep3/doctrine-extensions#configuring-slug-to-be-set-automatically – Baig

回答

1

这是一个天真的Symfony命令来重新生成给定类的所有蛞蝓:

<?php 

namespace App\Command; 

use App\Entity\Foo; 
use App\Entity\Bar; 
use Doctrine\Common\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; 

    public function __construct(ManagerRegistry $doctrine) 
    { 
     parent::__construct(); 

     $this->doctrine = $doctrine; 
    } 

    protected function configure(): void 
    { 
     $this 
      ->setName('app:regenerate-slugs') 
      ->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."); 
     } 
    } 
} 

嗨希望它可以帮助别人绊倒在这个问题!