Symfony Doctrine Sluggable可以在具有相同slug set的两个对象之间交换slug

时间:2014-08-03 09:59:21

标签: symfony doctrine-orm slug

我有多个具有相同段塞集的对象需要是唯一的。

如下所示

  • 对象ID:1 slug:object
  • 对象ID:2 slug:object-1
  • 对象ID:3 slug:object-2
  • 对象ID:4 slug:object-3

从上面的数据库条目中,我想"交换"两个物体之间的slu ..

例如,

  • 对象ID:1 slug:object-2
  • 对象ID:2 slug:object-1
  • 对象ID:3 slug:object
  • 对象ID:4 slug:object-3

我尝试过以下逻辑:

  1. 将对象1设置为随机' slu and并坚持然后冲洗(以便Object3可以采取主要的' slug)
  2. 将Object3设置为具有' object' slu and并坚持然后冲洗

    (此时,Object3接收' object-2' slug,好像Object1具有主要slug,object。)

    (当只有2个物体时,上面有效)

  3. 将Object1设置为NULL slug以获取自动生成的slug

  4. 任何建议都将非常感谢!如果有什么不够清楚,请发表评论!

    $product = $em->getRepository('EronkaCoreBundle:Product')->find($id);
    
        $mainProduct = $em->getRepository('EronkaCoreBundle:Product')->findProductsByVersion($product->getProductKey(), true);
        $mainProduct = $mainProduct[0];
    
        $mainSlug=$mainProduct->getSlug();
        $versionSlug=$product->getSlug();
    
        $mainProduct->setSlug('temporarySlug');
    
        // Persist temporary slug to be able to swap
        $mainProduct->setIsMain(false);
        $em->persist($mainProduct);
        $em->flush();
    
    // At this point, no object has the main slug 'object'
    
        // give proper main slug to the selected product
    
        $product->setSlug($mainSlug);
        $product->setIsMain(true);
        $em->persist($product);
        $em->flush();
    
        // Product slug not set as proper slug =(, it gets 'object2' slug as if the main slug 'object' was not available.
    
    
        // reset the slug
        $mainProduct->setSlug(NULL);
        $em->flush();
    

0 个答案:

没有答案