【问题标题】:Joint 2 Tables Together in CAKEPHP在 CAKEPHP 中将 2 个表连接在一起
【发布时间】:2015-03-11 10:16:07
【问题描述】:

大家好,我有 2 个表,itemsitem_translations,其中项目有字段

标识 |地址 |状态 |创建 |修改 | is_active

并且 item_translations 有字段

标识 | item_id |项目名称 | item_description |语言代码

所以它是一个多语言网站。

所以关系是

itemshasMany item_translations

item_translations 属于Item

所以我想做的是,当用户根据返回的语言选择一种语言时,他会返回每个属性的特定翻译行。

例如,带有 (1) 的项目被翻译成 3 种语言 it、en、de 当用户选择 en 时,它返回 id=x 的项目和 item_id=x 和 language_code=en 的 item_translations,但它应该只返回一行hasMany 关系。

所以到目前为止我的查询是:

$this->Paginator->settings =  array(
    'joins' => array(
        array(
            'table' => 'item_translations',
            'alias' => 'ItemTranslation',
            'type' => 'LEFT',
            'conditions' => array('Item.id = ItemTranslation.item_id')
        )
    ),
        'conditions' => array(
            'Item.item_status'=>1, 
            'User.is_actve'=>1,
            'ItemTranslation.language_code' => $language_code
        ),
        'limit' => 5,
        'order' => array('Item.sort' => 'ASC'),
        'recursive' => 1
);
$items = $this->paginate('Item');

但是这样做的问题是它返回了这个数组:

Array
(
    [0] => Array
        (
            [Item] => Array
                (
                    [id] => 17
                    [address] => Località Montecalvo di Sotto, 63, 02038 Scandriglia RI, Italy
                    [created] => 2015-03-09 17:13:53
                    [modified] => 2015-03-09 17:13:53
                    [is_active] => 
                    ---------
                )
            [ItemTranslation] => Array
                (
                    [0] => Array
                        (   
                            [id] => 78
                            [item_id] => 17
                            [language_code] => en
                            [item_name] => This is the title of this property in ventita
                            [item_description] => This is a module for tradure in other language diponibile
                        )

                    [1] => Array
                        (
                            [id] => 79
                            [item_id] => 17
                            [language_code] => de
                            [item_name] => Dies ist der Titel dieser Eigenschaft in Ventita
                            [item_description] => Dies ist ein Modul zum tradure in anderen Sprach diponibile

                        )

                    [2] => Array
                        (
                            [id] => 80
                            [item_id] => 17
                            [language_code] => 
                            [item_name] => Questo e il titolo da questo proprieta in ventita
                            [item_description] => Questo e una module per tradure in altre lingua diponibile
                        )

                )

        )
)

它返回与该项目相关的所有翻译,而不是只返回 language_code=en 或 de 的一行。

请任何帮助,我将不胜感激.. 提前感谢

【问题讨论】:

  • 使用containable behavior,而不是JOIN,关注deeper associations - 该示例模仿您的要求
  • @AgRizzo 感谢您的回答,但我要问另一件事,真是一个愚蠢的问题,在我的情况下您会怎么做,因为它似乎对我不起作用??
  • 为什么不起作用(您需要具体说明)?您的解决方案不会包括array('contain' => 'ItemTranslation.language_code = $language_code') 之类的内容吗?

标签: cakephp join cakephp-2.0 cakephp-2.1 cakephp-2.3


【解决方案1】:

将 language_code 移至 Joins 数组中的条件即可:

$this->Paginator->settings =  array(
'joins' => array(
    array(
        'table' => 'item_translations',
        'alias' => 'ItemTranslation',
        'type' => 'LEFT',
        'conditions' => array(
            'Item.id = ItemTranslation.item_id',
            'ItemTranslation.language_code' => $language_code
        )
    )
),
    'conditions' => array(
        'Item.item_status'=>1, 
        'User.is_actve'=>1
    ),
    'limit' => 5,
    'order' => array('Item.sort' => 'ASC'),
    'recursive' => 1
);
$items = $this->paginate('Item');

一般来说,如果您使用 Cake 的翻译行为,您可能会更轻松地进行翻译 - 很多繁重的工作已经为您完成:

http://book.cakephp.org/2.0/en/core-libraries/behaviors/translate.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多