我如何使用 Sonata 和 Knp Translatable Behavior 克隆已翻译的字段
How can i clone translated fields using Sonata and Knp Translatable Behavior
请问,我如何使用 Sf3 + SonataAdminBundle 和 Knp Trabnslatable 行为作为翻译策略来克隆一个对象及其可翻译字段内容。
我已经创建了一个自定义操作来克隆我的对象,除了可翻译的字段外,所有不可翻译的字段都已正确克隆和插入。
//Custom action to clone the object
public function cloneAction($id){
$object = $this->admin->getSubject();
if( !$object && !is_object( $object ))
{
throw new NotFoundHttpException( 'Enable to find the object with the id : '. $id );
}
$clonedObject = clone $object;
$ret = $this->admin->create( $clonedObject );
$clonedObject->translate()->setTitle( $object->translate()->getTitle(). ' (Cloned)' );
$this->addFlash( 'sonata_flash_success', 'Cloned successfully' );
return new RedirectResponse( $this->admin->generateUrl('list') );
}
在 php 中,当您克隆对象时,不会克隆引用。这就是所谓的浅拷贝。您需要做的就是克隆翻译对象。
<?php
foreach ($clonedObject->getTranslations() as $translation) {
$clonedObject->removeTranslation($translation);
$clonedObject->addTranslation(clone $translation);
}
请问,我如何使用 Sf3 + SonataAdminBundle 和 Knp Trabnslatable 行为作为翻译策略来克隆一个对象及其可翻译字段内容。
我已经创建了一个自定义操作来克隆我的对象,除了可翻译的字段外,所有不可翻译的字段都已正确克隆和插入。
//Custom action to clone the object
public function cloneAction($id){
$object = $this->admin->getSubject();
if( !$object && !is_object( $object ))
{
throw new NotFoundHttpException( 'Enable to find the object with the id : '. $id );
}
$clonedObject = clone $object;
$ret = $this->admin->create( $clonedObject );
$clonedObject->translate()->setTitle( $object->translate()->getTitle(). ' (Cloned)' );
$this->addFlash( 'sonata_flash_success', 'Cloned successfully' );
return new RedirectResponse( $this->admin->generateUrl('list') );
}
在 php 中,当您克隆对象时,不会克隆引用。这就是所谓的浅拷贝。您需要做的就是克隆翻译对象。
<?php
foreach ($clonedObject->getTranslations() as $translation) {
$clonedObject->removeTranslation($translation);
$clonedObject->addTranslation(clone $translation);
}