如何在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'));
}
答案 0 :(得分:0)
Cakebake一般是简单的蛋糕php方式。它从命令行
简单易用
制作2张桌子
答案 1 :(得分:0)
TranslateBehavior非常简单易用,因为它是在Model级别处理的。
将它添加到您的模型中:
public $actsAs = array(
'Translate' => array('fields','to','translate')
)
);
这将自动正确处理所有CRUD操作并将翻译存储在数据库中。
Pre-req是初始化i18n数据库表。请参阅此处的参考:http://book.cakephp.org/2.0/en/core-libraries/behaviors/translate.html