lundi 14 décembre 2015

insert multiple checkboxes into mysql database using cakephp

i have a problem trying to insert checkboxe values into 1 of my DB tables. I have a table users, services and categories all associated to the users table.My question is how to insert all the selected checkboxes at the form to be saved into the services table and once is done how to retrieve them. I could display the checkboxes along with services name but not saved them. I am new in cakephp and I will appreciated some help thank you in advance. services table

add.ctp

   echo this->Form->select('service_id',$services,array('multiple'=>'checkbox'));

usersController.php

if ($this->request->is('post')) {
        $this->User->create();
            //debug($this->request->data);
            //die();
        $this->request->data['User']['role'] = 'user';
        if ($this->User->saveAll($this->request->data)) {
            $this->Flash->success(__('The user has been saved.'));
            return $this->redirect(array('action' => 'index'));
       } else {
      $this->Flash->error(__('The user could not be saved. Please, try again.'));
        }
    }
   $services = $this->User->Service->find('list');
     $categories    =$this->User->Category->find('list',array('fields'=>'category'));
    $this->set(compact('categories','services'));

  }


 user.php

 public $hasMany = array(
       'Service' => array(
           'className' => 'Service',
        'foreignKey' => 'user_id',
        'dependent' => false,
        'conditions' => '',
        'fields' => '',
        'order' => '',
        'limit' => '',
        'offset' => '',
        'exclusive' => '',
        'finderQuery' => '',
        'counterQuery' => ''
    )

Once I run the debug() this is what i Got enter image description here




Aucun commentaire:

Enregistrer un commentaire