CakePHP 3:如何接收翻译内容?

时间:2015-09-15 08:54:55

标签: cakephp cakephp-3.0

我需要收到一个翻译后的内容才能填写表格。

控制器中的

public function translate($id = null)
    {
        $this->Apartments->locale('deu');
        $apartment = $this->Apartments->get($id);
        .....
        $this->set(compact('apartment'));
        $this->set('_serialize', ['apartment']);
    }

我还没有收到翻译的内容!

public function initialize(array $config)
    {
        parent::initialize($config);

        $this->table('apartments');
        $this->displayField('name');
        $this->primaryKey('id');

        $this->addBehavior('Timestamp');
        $this->addBehavior('Translate', ['fields' => ['description']]);
    }

接收翻译内容的正确方法是什么?我需要在Table和Entity Classes中添加什么才能使其正常工作?

更新 SQL查询

SELECT Apartments.id AS `Apartments__id`,
       Apartments.user_id AS `Apartments__user_id`,
       Apartments.name AS `Apartments__name`,
       Apartments.slug AS `Apartments__slug`,
       Apartments.description AS `Apartments__description`,
       Apartments.number_of_rooms AS `Apartments__number_of_rooms`,
       Apartments.number_of_beds AS `Apartments__number_of_beds`,
       Apartments.badroom AS `Apartments__badroom`,
       Apartments.surface AS `Apartments__surface`,
       Apartments.internet AS `Apartments__internet`,
       Apartments.air_condition AS `Apartments__air_condition`,
       Apartments.parking AS `Apartments__parking`,
       Apartments.pets AS `Apartments__pets`,
       Apartments.created AS `Apartments__created`,
       Apartments.modified AS `Apartments__modified`,
       Apartments.active AS `Apartments__active`,
       Apartments_description_translation.id AS `Apartments_description_translation__id`,
       Apartments_description_translation.locale AS `Apartments_description_translation__locale`,
       Apartments_description_translation.model AS `Apartments_description_translation__model`,
       Apartments_description_translation.foreign_key AS `Apartments_description_translation__foreign_key`,
       Apartments_description_translation.field AS `Apartments_description_translation__field`,
       Apartments_description_translation.content AS `Apartments_description_translation__content`
FROM apartments Apartments
LEFT JOIN i18n Apartments_description_translation ON (Apartments_description_translation.model = 'Apartments'
                                                      AND Apartments_description_translation.field = 'description'
                                                      AND Apartments_description_translation.locale = 'deu'
                                                      AND Apartments.id = (Apartments_description_translation.foreign_key))
WHERE Apartments.id = 1 LIMIT 1

1 个答案:

答案 0 :(得分:3)

你在评论中提到:

  

我使用现有数据库,其中包含基于CakePHP 2的应用程序输入的数据。 ...在i18n表中我有一个型号名称Apartment,但是cakephp3搜索公寓。

模型字段值是CakePHP 2中的模型名称(单数),以及CakePHP 3中的表名(复数形式)。

要解决问题,您可以:

迁移转换表中的数据

这是最合乎逻辑的解决方案,除非仍有CakePHP 2.x代码查看翻译记录,只需更新翻译记录:

UPDATE i18n set model = "Apartments" where model = "Apartment";
... etc ...

更改使用的参考

默认情况下,模型字段 是表类的名称,但它不必是configurable

public function initialize(array $config)
{
    parent::initialize($config);

    ...
    $this->addBehavior('Translate', [
        'fields' => ['description'],
        'referenceName' => 'Apartment' # <- 
    ]);
}

通过这种方式,CakePHP 3.x代码将与早期版本相同/兼容。