我有多个具有相同段塞集的对象需要是唯一的。
如下所示
从上面的数据库条目中,我想"交换"两个物体之间的slu ..
例如,
我尝试过以下逻辑:
将Object3设置为具有' object' slu and并坚持然后冲洗
(此时,Object3接收' object-2' slug,好像Object1具有主要slug,object。)
(当只有2个物体时,上面有效)
将Object1设置为NULL slug以获取自动生成的slug
任何建议都将非常感谢!如果有什么不够清楚,请发表评论!
$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();