翻译行为(查询超时)

时间:2013-05-30 16:59:40

标签: cakephp cakephp-1.3

我有一个模特:

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中有更好的方法吗?

1 个答案:

答案 0 :(得分:1)

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

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

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