douou5933
2016-07-20 13:10
浏览 31
已采纳

Symfony Doctrine现有数据库中的Sluggable扩展

I had to add in an existing entity a slug field to slugify the field 'name'. But there is already data in this entity and I can't delete them.

I would like to create a console script which can slugify all my 'name' field.

I don't know how to do it because this is not an insertion but just an update...

class SlugCommand extends ContainerAwareCommand
{
    protected function configure()
    {
        $this
            ->setName('generate:geo:slug')
            ->setDescription('Slug generation for GeoBundle ');
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $em = $this->getContainer()->get('doctrine')->getManager();
        $regions = $em->getRepository('FMGeoBundle:Region')->findAll();

        if($regions === null){
            throw new Exception('No Region found');
        }

        foreach($regions as $region){
            // ????? Generate the slug here ??
            $em->persist($region);
        }

        $em->flush();
        $output->writeln('Slugs Generated ;) ...');
    }
}

The 'slug' field in my entity:

/**
 * @var string
 *
 * @ORM\Column(name="slug", type="string", length=255)
 * @Gedmo\Slug(fields={"name"})
 */
protected $slug;

图片转代码服务由CSDN问答提供 功能建议

我必须在现有实体中添加一个slug字段来强制字段'name'。 但是这个实体中已有数据,我无法删除它们。

我想创建一个控制台脚本,它可以强化我所有的“名称”字段。 \ n

我不知道该怎么做,因为这不是插入而只是更新...

 类SlugCommand扩展ContainerAwareCommand 
 {\  n protected function configure()
 {
 $ this 
  - > setName('generate:geo:slug')
  - > setDescription('Sog generation for GeoBundle'); 
} 
 
  protected function execute(InputInterface $ input,OutputInterface $ output)
 {
 $ em = $ this-> getContainer() - > get('doctrine') - > getManager(); 
 $ regions = $  em-> getRepository('FMGeoBundle:Region') - > findAll(); 
 
 if if($ regions === null){
 throw new Exception('No Region found'); 
} \  n 
 foreach($ regions as $ region){
 // ????? 在这里生成slu ?? ?? ?? $ n $ em-> persist($ region); 
} 
 
 $ em-> flush(); 
 $ output-> writeln('Slugs Generated;)。  ..'); 
} 
} 
   
 
 

我的实体中的'slug'字段:

  / ** 
 * @var string 
 * 
 * @ORM \ Column(name =“slug”,type =“string”,length = 255)
 * @Gedmo \ Slug(fields = {“name  “})
 * / 
保护$ slug; 
   
 
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • doumi9618 2016-07-21 07:40
    已采纳

    I found an easier way. You can apparently just set the slug manually like that. And it will slugify the field needed.

    foreach ($regions as $region) {
         $region->setSlug($region->getName());
    
         $this->em->persist($region);
    }
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dongtan5558 2016-07-20 19:15

    The Sluggable extension works on both create and update actions. Therefore, you could just simulate an update by putting a row's name with its own.

    评论
    解决 无用
    打赏 举报
  • drymoeuka282427675 2016-07-26 16:12

    Just saw that the Gedmo library documentation directly answers this question :

    Regenerating slug

    In case if you want the slug to regenerate itself based on sluggable fields, set the slug to null.

    <?php $entity = $em->find('Entity\Something', $id);
    $entity->setSlug(null);
    
    $em->persist($entity); $em->flush();
    

    So, in your case you have just to persist the entity, nothing else. Because $slug is already null.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题