dongyan6910 2015-01-19 14:53
浏览 49
已采纳

如何使用Doctrine和Gedmo可翻译扩展来获取不同翻译中的对象

I'm stuck with the following problem: I want to fetch doctrine entities of a specific locale without breaking the default behavior of my Symfony app.

Here is an example of one of my entity:

use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;

/**
 * @ORM\Entity(repositoryClass="ProductRepository")
 * @ORM\Table(name="product")
 * @ORM\InheritanceType("SINGLE_TABLE")
 * @ORM\DiscriminatorColumn(name="discr", type="string")
 */
class Product
{
    /**
     * @var integer $id
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @var string
     * @ORM\Column(name="name", type="string")
     * @Gedmo\Translatable
     */
    protected $name;

    // ...
}

The a part of related doctrine repository:

class ProductRepository extends \Doctrine\ORM\EntityRepository
{
    public function findOneProductInLocale($id, $locale)
    {
        $qb = $this->createQueryBuilder('p')
            ->select('p')
            ->where('p.id = :id')
            ->setMaxResults(1)
            ->setParameter('id', $id);
        ;

        $query = $qb->getQuery();

        $query->setHint(
            \Doctrine\ORM\Query::HINT_CUSTOM_OUTPUT_WALKER,
            'Gedmo\\Translatable\\Query\\TreeWalker\\TranslationWalker'
        );

        // force Gedmo Translatable to not use current locale
        $query->setHint(
            \Gedmo\Translatable\TranslatableListener::HINT_TRANSLATABLE_LOCALE,
            $locale
        );

        $query->setHint(
            \Gedmo\Translatable\TranslatableListener::HINT_FALLBACK,
            1
        );

        return $query->getOneOrNullResult();
    }
}

and a part of my scripts:

// default Locale: en
// request Locale: de
$repo = $em->getRepository('Acme\\Entity\\Product');

$product1 = $repo->findOneById($id);
echo $product1->getName(); // return 'Name (DE)'

$product_de = $repo->findOneProductInLocale($id, 'de');
echo $product_de->getName(); // return 'Name (DE)';

$product_en = $repo->findOneProductInLocale($id, 'en');
echo $product_en->getName(); // return 'Name (EN)'

echo $product1->getName(); // return 'Name (EN)' instead of 'Name (DE)' !! <-- What is wrong?

// even if I refetch a product
$product2 = $repo->findOneById($id);
echo $product2->getName(); // return 'Name (EN)' without taking anymore in account the current locale

Is someone now why this didn't work as expected ? Is something wrong in my implementation of ProductRepository::findOneProductInLocale() ?

Any help or hint is welcome.

  • 写回答

3条回答 默认 最新

  • dongxin8392 2018-08-16 07:53
    关注

    I know I'm kind of late with my answer, but I face the same issue & found a solution. I hope it will help some other developers.

    1. Your findOneProductInLocale if perfectly fine.

      It works as design - when you will use findOneProductInLocale, the query will search in the given locale, but the final entity will always be loaded in the current locale, you can't change it.

    2. Once the entity has been found via findOneProductInLocale & loaded in the current locale, you can get the locale variant you need by using setTranslatableLocale method of Gedmo & refreshing your entity as explain by @umadesign

      // Reload the entity in different languages.
      $entity->setTranslatableLocale($locale);
      $em->refresh($entity);
      
    3. (Optional) You may need to add the setTranslatableLocale method & the companion property $local to your translatable entity

      class Product {
        // ...
      
        /**
          * @Gedmo\Locale
          * Used locale to override Translation listener`s locale
          * this is not a mapped field of entity metadata, just a simple property
          */
        private $locale;
      
        /**
          * Set the locale to use for translation listener
          *
          * @param string $locale
          *
          * @return static
          */
        public function setTranslatableLocale($locale) {
            $this->locale = $locale;
            return $this;
        }
      
        // ...
      }
      

    You can find the complete explanation in the Gedmo documentation under "Basic usage examples" subsection.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 simulink单相桥式整流电路
  • ¥35 问问51单片机流水灯的代码该怎么写
  • ¥15 关于#百度#的问题:感觉已经将字体段落、字体、页边距、纸张大小、文档网络调成与论文模板一致,为什么黄色部分字体左右的间距还是不一样啊,求私信发文件接收看一下
  • ¥15 stata webuse报错
  • ¥15 TypeError: Cannot read properties of undefined (reading 'status')
  • ¥15 如何利用AI去除图片中的竹架子
  • ¥15 python 写个基金爬取的代码,自动卖出功能
  • ¥15 Linux系统启动不起来
  • ¥15 为什么运行仿真数码管不亮(语言-c语言)
  • ¥15 陈仁良《直升机飞行动力学》小扰动线化方程如何推导