多重选择输入Cakephp [英] Multiple select in input Cakephp

查看:260
本文介绍了多重选择输入Cakephp的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个输入字段,其中有关于'customer_id'的数据请求生成的多个选项是

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

$ request->数据包含数组(CompanysCustomer => array 'id'=>'1','uid'=>'fhs32hrqwr8wfsdiof','customer_id'=> array('0'=>'5','1'=>'8','2'=>'9 '))。

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

我不要undestand如何使用或使用这个数组。工作很好,当我不使用多,因为我不会得到数组'customer_id '。我有一个想法来操作数组,并列出它们,保存pr foreach,但这似乎是无效的,它必须有一些其他的方式?

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?

im无法保存到db。当我有一个单一的记录保存,我设法使它。我不能让它一次保存许多记录。

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.

版本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

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

查看:CompanyCustomer\ add.ctp

View: CompanyCustomer\add.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>


推荐答案

阅读文档有助于
http://book.cakephp.org/2.0/en /core-libraries/helpers/form.html#FormHelper::select

'multiple' => true

使用

saveMany()

这篇关于多重选择输入Cakephp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆