如何添加、编辑、查看带有belongsTo等模型关系的translate行为,在cakephp中有很多

如何添加、编辑、查看带有belongsTo等模型关系的translate行为,在cakephp中有很多,php,cakephp-2.0,Php,Cakephp 2.0,如何在Cakephp-2.0中添加、编辑、查看带有模型关系的转换行为 我的添加代码: public function add() { if ($this->request->is('post')) { $this->Faq->create(); if ($this->Faq->saveMany($this->request->data)) { $this->Se

如何在Cakephp-2.0中添加、编辑、查看带有模型关系的转换行为

我的添加代码:

public function add() {

    if ($this->request->is('post')) {
        $this->Faq->create();        
        if ($this->Faq->saveMany($this->request->data)) {
            $this->Session->setFlash('The faq has been saved', 'default', array('class' => 'success'));
            $this->redirect(array('action' => 'index'));
        } else {
            $this->Session->setFlash(__('The faq could not be saved. Please, try again.'));
        }
    }
        $languages = $this->Language->getlangs();
        if(is_array($this->{$this->modelClass}->belongsTo)) {
            foreach($this->{$this->modelClass}->belongsTo as $relation => $model) {
                foreach($languages as $lang){
                    $this->{$this->modelClass}->$model['className']->locale = $lang['Language']['language_locale'];
                    $faqCategories[$lang['Language']['language_locale']] = $this->Faq->FaqCategory->find('list', array('conditions' => array('FaqCategory.is_active' => 1), 'recursive' => 1));
                }
            }
        }
        $this->set(compact('faqCategories'));


}
我的编辑代码:

public function edit($id = null) {

    if (!$this->Faq->exists($id)) {
        throw new NotFoundException(__('Invalid faq'));
    }
    if ($this->request->is('post') || $this->request->is('put')) {
        if ($this->Faq->save($this->request->data)) {
            $this->Session->setFlash('The faq has been saved', 'default', array('class' => 'success'));
            $this->redirect(array('action' => 'index'));
        } else {
            $this->Session->setFlash(__('The faq could not be saved. Please, try again.'));
        }
    } else {
        $options = array('conditions' => array('Faq.' . $this->Faq->primaryKey => $id));
        $this->request->data = $this->Faq->find('first', $options);
    }
    $faqCategories = $this->Faq->FaqCategory->find('list', array('conditions' => array('FaqCategory.is_active' => 1)));
    $this->set(compact('faqCategories'));
}

Cakebake在蛋糕php中通常是一种简单的方法。从命令行使用它非常简单

做两张桌子

  • department和2是depart id中的student so student表,作为外键关系

  • TranslateBehavior的使用非常简单,因为它是在模型级别处理的

    将其添加到模型中,如下所示:

    public $actsAs = array(
        'Translate' => array('fields','to','translate')
        )
    );
    
    这将自动正确处理所有CRUD操作,并将翻译存储在db中

    Pre-req用于初始化i18n数据库表。参见此处的ref: