用CAKEPHP将两个表连接在一起
嘿,伙计们,我有两个表,items和item_翻译,其中item有字段 id |地址|状态|已创建|已修改|处于活动状态 并且项有字段 id |项目|项目|名称|项目|描述|语言|代码 所以它是一个多语言网站 所以关系是 items有许多item\u翻译 项目\属于项目的翻译 所以我想做的是,当用户根据语言选择语言时,他返回返回每个属性的特定翻译行 例如,带有1的项被翻译成3种语言it、en、de当用户选择en时,它返回id=x的项,以及id=x、语言代码=en的项,但它应该只返回一行hasMany关系 因此,到目前为止,我的问题是:用CAKEPHP将两个表连接在一起,cakephp,join,cakephp-2.0,cakephp-2.1,cakephp-2.3,Cakephp,Join,Cakephp 2.0,Cakephp 2.1,Cakephp 2.3,嘿,伙计们,我有两个表,items和item_翻译,其中item有字段 id |地址|状态|已创建|已修改|处于活动状态 并且项有字段 id |项目|项目|名称|项目|描述|语言|代码 所以它是一个多语言网站 所以关系是 items有许多item\u翻译 项目\属于项目的翻译 所以我想做的是,当用户根据语言选择语言时,他返回返回每个属性的特定翻译行 例如,带有1的项被翻译成3种语言it、en、de当用户选择en时,它返回id=x的项,以及id=x、语言代码=en的项,但它应该只返回一行hasMa
$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提前将语言代码移动到联接数组中的条件应该可以:
$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的翻译行为,你可能会更容易进行翻译,但很多繁重的工作已经为你完成了:
不要使用JOIN,而是使用,focus on-示例模仿您的requirement@AgRizzo谢谢你的回答,但我要问另一件事,一个愚蠢的问题,真的,在我的情况下,你会怎么做?因为我觉得它不管用??为什么它不管用?你需要具体一点?您的解决方案不包括类似于数组'contain'=>'itemtransation.language\u code=$language\u code'的内容吗?