首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在cakephp中使用带有模型关系的翻译行为(如belongsTo、hasMany )来添加、编辑、查看

如何在cakephp中使用带有模型关系的翻译行为(如belongsTo、hasMany )来添加、编辑、查看
EN

Stack Overflow用户
提问于 2014-01-15 22:14:13
回答 2查看 162关注 0票数 1

如何在Cakephp-2.0中使用带有模型关系的翻译行为来添加、编辑、查看?

我的add代码:

代码语言:javascript
复制
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'));


}

我的编辑代码:

代码语言:javascript
复制
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'));
}
EN

回答 2

Stack Overflow用户

发布于 2014-01-24 13:56:29

Cakebake通常是蛋糕php中的简单方法。而且从命令行使用它也很简单

制作2张表格

学生部门和2是

  1. ,因此部门id中的学生表为外键关系

票数 0
EN

Stack Overflow用户

发布于 2015-02-10 17:26:16

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

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

代码语言:javascript
复制
public $actsAs = array(
    'Translate' => array('fields','to','translate')
    )
);

这将自动正确地处理所有CRUD操作,并将翻译存储在数据库中。

前置请求是初始化i18n数据库表。请参阅此处参考:http://book.cakephp.org/2.0/en/core-libraries/behaviors/translate.html

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21139720

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档