Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
606 views
in Technique[技术] by (71.8m points)

php - Multiple select in input Cakephp

WHen i have an input field with the multiple options concerning 'customer_id' the data request generated is

$request->data containing array(CompanysCustomer => array('id' => '1', 'uid' => 'fhs32hrqwr8wfsdiof', 'customer_id' => array('0' => '5', '1' => '8', '2' => '9')).

I dont undestand how to use or make use of this array. It Works well when i dont use multiple, because then i dont get array at 'customer_id'. I have an idea to manipulate the array and list them out ond do the save pr foreach, but that seems unefficient, it has to be some other way?

And im having trouble saving this to the db. I managed to make it when i have one single record to save. I cant make it save many records at once.

Version 2.4.1

public function add($id) {
  if ($this->request->is('post')) {
    $this->CompanysCustomer->create();
    if ($this->CompanysCustomer->save($this->request->data)){
      $this->Session->setFlash(__('The Companys Customer has been saved.'));
      return $this->redirect(array('action' => 'index'));
    }
  else{
    $this->Session->setFlash(__('The Companys Customer could not be saved. Please, try    again.'));}
  }

  $customers = $this->CompanysCustomer->Customer->find('list');
  $this->set(compact('customers'));
  }

Model CompanysCustomer

  <?php
  App::uses('AppModel', 'Model');
  /**
  * CompanysCustomer Model

  */
  class CompanysCustomer extends AppModel {
    //The Associations below have been created with all possible keys, those that are not needed can be removed

    /**
    * belongsTo associations
    *
    * @var array
    */
    public $belongsTo = array(
        'Company' => array(
            'className' => 'Company',
            'foreignKey' => 'company_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        ),
        'Customer' => array(
            'className' => 'Customer',
            'foreignKey' => 'customer_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        )
    );
}

View: CompanyCustomeradd.ctp

<div class="companysCustomers form">
    <?php echo $this->Form->create('CompanysCustomer'); ?>
    <fieldset>
        <legend><?php echo __('Add Companys Customer'); ?></legend>
        <?php
        $company_id = $id;
        echo $this->Form->input('company_id', array(
            'type' => 'hidden',
            'value' => $id,
        ));
        $uid = uniqid("", $more_entropy = true);
        echo $this->Form->input('uid', array(
            'type' => 'hidden',
            'value' => $uid,
        ));
        //echo $this->Form->input('company_id');
        echo $this->Form->input('customer_id',array('type' => 'select', 'multiple' => 'checkbox','size' => '20'));

        ?>
    </fieldset>
    <?php echo $this->Form->end(__('Submit')); ?>
</div>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Reading the documentation helps http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#FormHelper::select

'multiple' => true

for example.

For saving use

saveMany()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...