在HABTM中显示复选框附近的字体图标

时间:2015-02-26 17:43:33

标签: cakephp cakephp-2.0 cakephp-2.1 cakephp-2.3 cakephp-appmodel

我有4张桌子。

属性特征 characteristics_properties characteristic_translations

属性HABTM特征和反之亦然。

另外特征有很多特征转换和特征转换属于特性所以这里给出一个更清晰的例子是表格图:

enter image description here

所以我需要的是当我创建一个属性来显示特征列表时:

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

1 个答案:

答案 0 :(得分:0)

您需要在$characteristics列表变量中获取更多信息。

而不是:

$characteristics = $this->Property->Characteristic->find('list');

查找包含所需语言的所有内容:

$characteristics = $this->Property->Characteristic->find('all', array(
    'contain' => array(
        'CharacteristicTranslation' => array(
            'conditions' => array(
                'language_id' => 1
            )
        )
    )
));

然后为视图构建列表:

$tmp = array();
foreach($characteristics as $characteristic) {
    $tmp[$characteristic['Characteristic']['id']] = $characteristic['CharacteristicTranslation'][0]['characteristic_name'] . ' <i class="' .  $characteristic['Characteristic']['icon-class'] . '"></i>';
}
$characteristics = $tmp;
$this->set(compact('characteristics'));

在您的媒体资源表单中,您可以保留所拥有的内容,但您需要在选项中添加'escape' => false,因为其中没有HTML。