2

I have 4 tables.

properties, characteristics, characteristics_properties, characteristic_translations.

Properties HABTM characteristics and Vice Versa.

Also characteristics has many characteristic_translation and characteristic_translation belongs to characteristic so to give a more clear example here is the tables diagram:

enter image description here

So what I need is that when I create a property to display a list of characteristics:

characteristic_name_1 (icon1) [] checkbox1

characteristic_name_2 (icon2) [] checkbox2

characteristic_name_3 (icon3) [] checkbox3

So in the add view I have:

<?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(); 
?>  

The property Controller is:

/**
 * 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'));
    }
apaul
  • 16,092
  • 8
  • 47
  • 82
Johnny
  • 175
  • 1
  • 9
  • I think this answer will help you: http://stackoverflow.com/questions/22910137/display-image-next-to-habtm-checkboxes – mart Feb 26 '15 at 20:34
  • @mart Thanks for your replay, i have seen that answer but the problem with it is that it doesn't save the HABTM data correctly, or to be more correct it doesn't save them at all, also that example is for a simple HABTM relationship but on this case as you can see to the DB diagram i have to be able to show them on multilingual approach.... – Johnny Feb 27 '15 at 08:14

1 Answers1

0

You need to get more information into your $characteristics list variable.

Instead of:

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

Do a find all containing the language you want:

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

Then build your list for the view:

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

In your property form, you can keep what you have, except you need to add 'escape' => false to the options since there's no HTML in it.

Corie Slate
  • 616
  • 9
  • 19