【发布时间】:2015-02-26 17:43:33
【问题描述】:
我有 4 张桌子。
properties、characteristics、characteristics_properties、characteristic_translations。
属性 HABTM 特征和反之亦然。
还有很多characteristics_translation和characteristic_translation属于characteristic,所以为了更清楚的例子,这里是表格图:
所以我需要的是,当我创建一个属性来显示特征列表时:
characteristic_name_1 (icon1) [] checkbox1
characteristic_name_2 (icon2) [] checkbox2
characteristic_name_3 (icon3) [] checkbox3
所以在添加视图中我有:
<?php
echo $this->Form->create('Property', array(class'=>'form-horizontal'));
echo $this->Form->input('title', array('class'=>'form-control property-select'));
echo $this->Form->input('Characteristic', array('type'=>'select', 'multiple'=>'checkbox','options' => $characteristics,'selected' => $this->Html->value('Characteristic.Characteristic')));
echo $this->Form->end();
?>
属性Controller是:
/**
* add method
*
* @return void
*/
public function add() {
if ($this->request->is('post')) {
//debug($this->request->data);
$this->Property->create();
$this->request->data['Property']['user_id'] = $this->Auth->user('id');
if ($this->Property->save($this->request->data)) {
$this->Session->setFlash(__('The property has been saved.'),'flash_success');
return $this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The property could not be saved. Please, try again.'),'flash_error');
}
}
$users = $this->Property->User->find('list');
$currencies = $this->Property->Currency->find('list');
$roomTypes = $this->Property->RoomType->find('list', array('conditions' => array('RoomType.language_id'=>2), 'recursive'=>-1));
$accommodationTypes = $this->Property->AccommodationType->find('list', array('conditions' => array('AccommodationType.language_id'=>2), 'recursive'=>-1));
$houseAvailabilities = $this->Property->HouseAvailability->find('list', array('conditions' => array('HouseAvailability.language_id'=>2), 'recursive'=>-1));
$deleteReasons = $this->Property->DeleteReason->find('list', array('conditions' => array('DeleteReason.language_id'=>2), 'recursive'=>-1));
$characteristics = $this->Property->Characteristic->find('list');
$extras = $this->Property->Extra->find('list');
$safeties = $this->Property->Safety->find('list');
$services = $this->Property->Service->find('list');
$this->set(compact('users', 'currencies', 'roomTypes', 'accommodationTypes', 'houseAvailabilities', 'deleteReasons', 'characteristics', 'extras', 'safeties', 'services'));
}
【问题讨论】:
-
我认为这个答案会对你有所帮助:stackoverflow.com/questions/22910137/…
-
@mart 感谢您的重播,我已经看到了这个答案,但问题是它没有正确保存 HABTM 数据,或者更正确的是它根本没有保存它们,该示例也适用于简单的 HABTM 关系,但在这种情况下,正如您在 DB 图上看到的那样,我必须能够以多语言方法显示它们......
标签: cakephp cakephp-2.0 cakephp-2.1 cakephp-2.3 cakephp-model