dongpiao1983 2015-12-14 16:37
浏览 41

使用cakephp将多个复选框插入mysql数据库

I have a problem trying to insert checkbox values into 1 of my DB tables. I have a table users, services and categories all associated to the users table. How can I insert all the selected checkboxes at the form to be saved into the services table and then retrieve them? I could display the checkboxes along with services name but not save them.

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 ran the debug() this is what I got:

enter image description here

  services.php 

public $displayField = 'name';


       public $validate = array(
    'name' => array(
        'notBlank' => array(
            'rule' => array('notBlank'),
            //'message' => 'Your custom message here',
            //'allowEmpty' => false,
            //'required' => false,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
        ),
    ),
    'selected' => array(
        'boolean' => array(
            'rule' => array('boolean'),
            //'message' => 'Your custom message here',
            //'allowEmpty' => false,
            //'required' => false,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
        ),
    ),
    'user_id' => array(
        'numeric' => array(
            'rule' => array('numeric'),
            //'message' => 'Your custom message here',
            //'allowEmpty' => false,
            //'required' => false,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
        ),
    ),
);



public $belongsTo = array(
    'User' => array(
        'className' => 'User',
        'foreignKey' => 'user_id',
        'conditions' => '',
        'fields' => '',
        'order' => ''
    )
);


public $hasMany = array(
    'User' => array(
        'className' => 'User',
        'foreignKey' => 'service_id',
        'dependent' => false,
        'conditions' => '',
        'fields' => '',
        'order' => '',
        'limit' => '',
        'offset' => '',
        'exclusive' => '',
        'finderQuery' => '',
        'counterQuery' => ''
    )
);
/*public $hasAndBelongsToMany = array(
    'Platillo' => array(
        'className' => 'Service',
        'joinTable' => 'users_services',
        'foreignKey' => 'user_id',
        'associationForeignKey' => 'service_id',
        'unique' => 'keepExisting',
        'conditions' => '',
        'fields' => '',
        'order' => '',
        'limit' => '',
        'offset' => '',
        'finderQuery' => '',
    )
);*/     
  • 写回答

0条回答 默认 最新

    报告相同问题?