CAKEPHP共同联合2个表格

时间:2015-03-11 10:16:08

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

嘿伙计我有2个表,itemsitem_translations其中item有字段

id |地址|状态|创建|修改了| is_active

和item_translations有字段

id | item_id | item_name | item_description | LANGUAGE_CODE

所以这是一个多语种网站。

所以关系是

items hasMany item_translations

item_translations belongsTo Item

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

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

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

$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的一行,而是返回与此项目相关的所有翻译。

请帮助我真的很感激.. Thanx提前

1 个答案:

答案 0 :(得分:0)

将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