【问题标题】:CakePHP Translate behavior and pagination countCakePHP 翻译行为和分页计数
【发布时间】:2011-12-07 11:51:18
【问题描述】:

我有一个模型,它具有附加两个字段的翻译行为:标题和描述。 我在可翻译字段中添加了一些条件。 与分页一样,CakePHP 首先进行计数,然后获取所有记录。 获取查询的总记录时:

SELECT COUNT(DISTINCT( `product`.`id` )) AS COUNT
FROM   `products` AS `product`
   INNER JOIN `i18n` AS `i18nmodel`
     ON ( `product`.`id` = `i18nmodel`.`foreign_key`
          AND `i18nmodel`.`model` = 'Product'
          AND `i18nmodel`.`locale` = 'eng' )
   LEFT JOIN `categories` AS `category`
     ON ( `product`.`category_id` = `category`.`id` )
   LEFT JOIN `vats` AS `vat`
     ON ( `product`.`vat_id` = `vat`.`id` )
   LEFT JOIN `availables` AS `available`
     ON ( `product`.`available_id` = `available`.`id` )
WHERE  ( ( `i18n__description`.`content` LIKE '%test%' )
      OR ( `i18n__title`.`content` LIKE '%test%' )
      OR ( `product`.`code` LIKE '%test%' ) )  

我明白了:

1054: Unknown column 'I18n__description.content' in 'where clause'

因为 i18n 表不是作为 i18n_title 或 i18n_description 而是作为 i18nmodel 加入的

但是,当分页尝试检索查询的行(而不是总记录)时,一切正常。有什么解决办法吗?

控制器代码如下所示:

$condition = array();
foreach ($search as $word) {
if (strlen($word) > 0)
 $condition[] = array('OR' => array('I18n__description.content LIKE' => '%' . $word . '%','I18n__title.content LIKE' => '%' . $word . '%',
                        'Product.code LIKE' => '%' . $word . '%'));
}

$conditions = array('AND' => $condition);
$products = $this->paginate($conditions);

【问题讨论】:

  • 你能发布你的控制器代码吗?
  • 我添加了条件部分。

标签: cakephp internationalization pagination


【解决方案1】:

这种情况可以通过在要分页的模型中创建一个自定义的 paginateCount 方法来简单地处理。

function paginateCount($conditions = array(), $recursive = null, $extra = array()) {
    return count($this->find('all', array('conditions' => $conditions)))
}

【讨论】:

    【解决方案2】:

    分页计数器不使用翻译连接,因此您必须在查找之前手动连接。 FindAll 不是很好的解决方案,因为它运行复杂的查询,并且占用服务器资源。

    https://github.com/cakephp/cakephp/issues/1753

    解决方案。它不适用于 SmoothTranslate Behavior,但对上面的代码稍作修改,它就可以工作。 覆盖之前的查找。

     function beforeFind($queryData){
         if(!empty($this->actsAs['SmoothTranslate']['fields'])){
            foreach($this->actsAs['SmoothTranslate']['fields'] as $trkey=>$trval){
              if(!is_string($trkey))  $trkey=$trval;
    
              $joinFound=false;
              foreach($queryData['joins'] as $join){
                if($join['alias']=='I18n__'.$trkey){
                  $joinFound=true;  break;
                }
              }
              if(!$joinFound){
                $newJoin = array(
                  'type'=>'LEFT',
                  'table'=>'i18n',
                  'alias'=>'I18n__'.$trkey,
                  'conditions'=>array(
                    $this->alias.'.id'=>Set::map(array(
                      'type'=>'identifier',
                      'value'=>'I18n__'.$trkey.'.foreign_key',
                    )),
                    'I18n__'.$trkey.'.model'=>$this->alias,
                    'I18n__'.$trkey.'.locale'=>$this->locale,
                    'I18n__'.$trkey.'.field'=>$trkey,
                  ),
                );
                array_push($queryData['joins'], $newJoin);
              }
            }
    
        }
        return $queryData;
      }
    }
    

    【讨论】:

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