1

我有一个模型:

class Product extends AppModel {    
    var $actsAs=array(      
        'Translate' => array(
        'title', 'excerpt', 'overview', 'construction', 'options', 'features', 'benefits'
        )
    );      
}

在我的控制器中,我正在查询:

$this->Product->findPublic('all');

似乎默认行为是为查询中的每个翻译字段添加一个别名作为其自己的表,例如:

SELECT `Product`.*, `I18n__title`.`content`, `I18n__excerpt`.`content`, `I18n__overview`.`content`, `I18n__construction`.`content`, `I18n__options`.`content`, `I18n__features`.`content`, `I18n__benefits`.`content` FROM `ck_products` AS `Product` LEFT JOIN `ck_i18n` AS `I18n__title` ON (`Product`.`id` = `I18n__title`.`foreign_key` AND `I18n__title`.`model` = 'Product' AND `I18n__title`.`field` = 'title') LEFT JOIN `ck_i18n` AS `I18n__excerpt` ON (`Product`.`id` = `I18n__excerpt`.`foreign_key` AND `I18n__excerpt`.`model` = 'Product' AND `I18n__excerpt`.`field` = 'excerpt') LEFT JOIN `ck_i18n` AS `I18n__overview` ON (`Product`.`id` = `I18n__overview`.`foreign_key` AND `I18n__overview`.`model` = 'Product' AND `I18n__overview`.`field` = 'overview') LEFT JOIN `ck_i18n` AS `I18n__construction` ON (`Product`.`id` = `I18n__construction`.`foreign_key` AND `I18n__construction`.`model` = 'Product' AND `I18n__construction`.`field` = 'construction') LEFT JOIN `ck_i18n` AS `I18n__options` ON (`Product`.`id` = `I18n__options`.`foreign_key` AND `I18n__options`.`model` = 'Product' AND `I18n__options`.`field` = 'options') LEFT JOIN `ck_i18n` AS `I18n__features` ON (`Product`.`id` = `I18n__features`.`foreign_key` AND `I18n__features`.`model` = 'Product' AND `I18n__features`.`field` = 'features') LEFT JOIN `ck_i18n` AS `I18n__benefits` ON (`Product`.`id` = `I18n__benefits`.`foreign_key` AND `I18n__benefits`.`model` = 'Product' AND `I18n__benefits`.`field` = 'benefits')  WHERE `Product`.`status` = 'active' AND NOT (`Product`.`slug` = '') AND `I18n__title`.`locale` = 'eng' AND `I18n__excerpt`.`locale` = 'eng' AND `I18n__overview`.`locale` = 'eng' AND `I18n__construction`.`locale` = 'eng' AND `I18n__options`.`locale` = 'eng' AND `I18n__features`.`locale` = 'eng' AND `I18n__benefits`.`locale` = 'eng'   

我添加的字段越多,我的查询就越慢。事实上,它现在已经超时了。有没有更好的方法在 Cake 中做到这一点?

4

1 回答 1

1

我使用行为只返回一些解决我的问题的字段:

$this->Product->Behaviors->attach('Translate', array('title'));

如果您有 20 个翻译字段并要求将它们全部列在一个视图中,我仍然会对答案感兴趣。

于 2013-05-30T17:44:53.817 回答