简体   繁体   中英

how to add, edit, view using translate behaviour with model relationship like belongsTo, hasMany in cakephp

How to add, edit, view using Translate Behavior with model relationship in Cakephp-2.0 ?

My code for add:

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'));


}

My code for edit:

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 is generally easy way in cake php. and it vey simple using from command line

在此处输入图片说明

make 2 tables

  1. departement and 2 is student so student table in dept id as foreign key relationships

TranslateBehavior is very simply to use because it is handled at the Model level.

Add it to your model like this:

public $actsAs = array(
    'Translate' => array('fields','to','translate')
    )
);

This will automatically handle all CRUD operations properly and store translations in the db.

Pre-req is to initialize the i18n database tables. See ref here: http://book.cakephp.org/2.0/en/core-libraries/behaviors/translate.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM