3

我在网上搜索了 1000000000000000 次,但我找不到一个干净的解决方案

这是我的 CertificateType 模型翻译部分:

public $actsAs = array('Translate'=>array('title','description')) ;

和证书模型:

public $actsAs=array('Translate'=>array('filename')) ;

    public $belongsTo = array(
    'CertificateType' => array(
        'className' => 'CertificateType',
        'foreignKey' => 'certificate_type_id',
        'conditions' => '',
        'fields' => '',
        'order' => ''
    ) ,
   );

但是在获取时,所属模型不会翻译:

    public function admin_index() {
    $this->Certificate->locale = $this->Session->read('Config.language');
    $this->Certificate->CertificateType->locale =  $this->Session->read('Config.language');
    $this->Certificate->recursive = 0;
    $this->set('certificates', $this->paginate());
    debug($this->Certificate->paginate()) ;
}

为什么?

4

7 回答 7

6

我用过这个,效果很好!

在 AppModel.php 我写了这些代码:

    public function getTranslatedModelField($id = 0, $field) {
    // retrieve active language
    $ActiveLanguageCatalog=CakeSession::read('Config.ActiveLanguageCatalog') ;
    $res = false;
    $translateTable = (isset($this->translateTable))?$this->translateTable:"i18n";

    $db = $this->getDataSource();
    $tmp = $db->fetchAll(
        "SELECT content from {$translateTable} WHERE model = ? AND locale = ? AND foreign_key = ? AND field = ? LIMIT 1",
        array($this->alias, $ActiveLanguageCatalog['locale'], $id, $field)
    );
    if (!empty($tmp)) {
        $res = $tmp[0][$translateTable]['content'];
    }
    return $res;
}   

public function afterFind($results, $primary = false) {

    if($primary == false && array_key_exists('Translate', $this->actsAs)) {
        foreach ($results as $key => $val) {
            if (isset($val[$this->name]) && isset($val[$this->name]['id'])) {
                foreach($this->actsAs['Translate'] as $translationfield) {  
                    $results[$key][$this->name][$translationfield] = $this->getTranslatedModelField($val[$this->name]['id'], $translationfield);
                }
            } else if($key == 'id' && is_numeric($val)) {
                foreach($this->actsAs['Translate'] as $translationfield) {  
                    $results[$translationfield] = $this->getTranslatedModelField($val, $translationfield);
                }                   
            }
         }
    }

    return $results;
}
于 2013-12-30T16:53:44.140 回答
1

将以下代码添加到 AppModel:

public function afterFind($results, $primary = false) {
    if(!empty($this->hasMany)) {
        foreach($this->hasMany as $model => $settings) {
            if(isset($this->{$model}->actsAs['Translate'])) {
                if(!empty($results[0][$model])) {
                    foreach($results[0][$model] as $row => $result) {
                        $supplement = $this->{$model}->find('first', array(
                            'conditions' => array(
                                $model .'.id' => $result['id']),
                            'fields' => $this->{$model}->actsAs['Translate'],
                            'recursive' => -1));

                        if(!empty($supplement)) {
                            $results[0][$model][$row] = array_merge($results[0][$model][$row], array_diff($supplement[$model], $result));
                        }
                    }
                }
            }
        }
    }

    if(!empty($this->belongsTo)) {
        foreach($this->belongsTo as $model => $settings) {
            if(isset($this->{$model}->actsAs['Translate'])) {
                if(!empty($results[0][$model])) {
                    foreach($results[0][$model] as $row => $result) {
                        $supplement = $this->{$model}->find('first', array(
                            'conditions' => array(
                                $model .'.id' => $result),
                            'fields' => $this->{$model}->actsAs['Translate'],
                            'recursive' => -1));

                        if(!empty($supplement)) {
                            $results[0][$model] = array_merge($results[0][$model], $supplement[$model]);
                        }
                    }
                }
            }
        }
    }

    return $results;
} 

这适用于关系 hasMany 和 belongsTo

于 2013-09-12T13:05:32.973 回答
1

感谢 Vahid Alimohamadi 的剪辑。我必须修改它才能在我的项目中正常工作。

public function getTranslatedModelField($id = 0, $field) {
    // retrieve active language
    //$ActiveLanguageCatalog=CakeSession::read('Config.ActiveLanguageCatalog') ;
    $locale = Configure::read('Config.language');
    $res = false;
    $translateTable = (isset($this->translateTable))?$this->translateTable:"i18n";

    $db = $this->getDataSource();
    $tmp = $db->fetchAll(
        "SELECT content from {$translateTable} WHERE model = ? AND locale = ? AND foreign_key = ? AND field = ? LIMIT 1",
        array($this->alias, $locale/*$ActiveLanguageCatalog['locale']*/, $id, $field)
        );
    if (!empty($tmp)) {
        $res = $tmp[0][$translateTable]['content'];
    }
    return $res;
}

public function afterFind($results, $primary = false) {

    if($primary == false && array_key_exists('Translate', $this->actsAs)) {
        foreach ($results as $key => $val) {
            if (isset($val[$this->name]) && isset($val[$this->name]['id'])) {
                //HERE ADDED $field => $translationfield AND PASS IT TO THE getTranslatedModelField
                foreach($this->actsAs['Translate'] as $field => $translationfield) {
                    $results[$key][$this->name][$field] = $this->getTranslatedModelField($val[$this->name]['id'], $field);
                }
            } else if($key == 'id' && is_numeric($val)) {
                //HERE ADDED $field => $translationfield AND PASS IT TO THE getTranslatedModelField
                foreach($this->actsAs['Translate'] as $field => $translationfield) {
                    $results[$field] = $this->getTranslatedModelField($val, $field);
                }
            }
        }
    }

    return $results;
}
于 2014-02-12T13:16:33.913 回答
0

我对使用 cakephp 2.5 有一些更新。它现在对我有用。希望能有所帮助。

公共函数 getTranslatedModelField($id = 0, $field) {

    $res = false;
    $translateTable = (isset($this->translateTable))?$this->translateTable:"i18n";

    $db = $this->getDataSource();
    $tmp = $db->fetchAll(
        "SELECT content from {$translateTable} WHERE model = ? AND locale = ? AND foreign_key = ? AND field = ? LIMIT 1",
        array($this->alias, $this->locale , $id, $field)
    );

    if (!empty($tmp)) {
        $res = $tmp[0][$translateTable]['content'];
    }
    return $res;
}

public function afterFind($results, $primary = false) {

    if($primary == false && array_key_exists('Translate', $this->actsAs)) {
        foreach ($results as $key => $val) {
            if (isset($val[$this->name]) && isset($val[$this->name]['id'])) {
                foreach($this->actsAs['Translate'] as $k =>  $translationfield) {
                    $results[$key][$this->name][$translationfield] = $this->getTranslatedModelField($val[$this->name]['id'], $k);
                }
            } else if($key == 'id' && is_numeric($val)) {
                foreach($this->actsAs['Translate'] as $k =>  $translationfield) {
                    $results[$translationfield] = $this->getTranslatedModelField($val, $k);
                }
            }
        }
    }

    return $results;
}
于 2014-12-11T10:20:10.377 回答
0

我对获取当前语言进行了更改,并且与 cacke 2.4.3 完美配合

public function getTranslatedModelField($id = 0, $field) {
    // retrieve active language

    $locale = $this->locale ;// gets the current assigned locale

    $res = false;
    $translateTable = (isset($this->translateTable))?$this->translateTable:"i18n";

    $db = $this->getDataSource();
    $tmp = $db->fetchAll(
        "SELECT content from {$translateTable} WHERE model = ? AND locale = ? AND foreign_key = ? AND field = ? LIMIT 1",
        array($this->alias, $locale , $id, $field)
    );
    if (!empty($tmp)) {
        $res = $tmp[0][$translateTable]['content'];
    }
    return $res;
}   

public function afterFind($results, $primary = false) {

    if($primary == false && array_key_exists('Translate', $this->actsAs)) {
        foreach ($results as $key => $val) {
            if (isset($val[$this->name]) && isset($val[$this->name]['id'])) {
                foreach($this->actsAs['Translate'] as $translationfield) {  
                    $results[$key][$this->name][$translationfield] = $this->getTranslatedModelField($val[$this->name]['id'], $translationfield);
                }
            } else if($key == 'id' && is_numeric($val)) {
                foreach($this->actsAs['Translate'] as $translationfield) {  
                    $results[$translationfield] = $this->getTranslatedModelField($val, $translationfield);
                }                   
            }
         }
    }

    return $results;
}
于 2014-01-09T14:09:32.957 回答
-1

通过使用 Model->SubModel->Behaviors->load(...) 以 Cake 方式为相关模型启用翻译行为非常容易

$this->Certificate->CertificateType->Behaviors->load(
    'Translate',
     array('title','description')
     );

看看http://book.cakephp.org/2.0/en/models/behaviors.html

于 2014-06-03T12:24:04.533 回答
-1

最简单的解决方案是以下获取关联模型项的 id 并进行另一次搜索,如下所示:

            $ids = array();
    foreach ($wpage['Widget'] as &$widget):
        $ids[] = $widget['id'];
    endforeach;

    $widgets = $this->Wpage->Widget->find('all', array(
        'conditions' => array('Widget.id' => $ids)
    ));

    $this->set(compact('wpage', 'widgets'));    

谢谢,阿里斯

于 2013-12-30T01:44:18.507 回答