3

Webを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

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 での作業を更新しました。それは今私のために働いています。希望が助けになります。

public function 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 に答える
-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 に答える