如果我有一个输入字段,其中包含有关'customer_id'的多个选项,则生成的数据请求为
$ request->包含数组的数据(CompanysCustomer => array('id'=>'1','uid'=>'fhs32hrqwr8wfsdiof','customer_id'=>数组('0'= >'5','1'=>'8','2'=>'9'))。
我不知道如何使用或使用这个数组。当我不使用多个时,它运行良好,因为那时我没有在'customer_id'获得数组。我有一个想法来操纵数组并将它们列出来执行save pr foreach,但这看起来效率不高,它必须是其他方式吗?
我无法将此保存到数据库。当我有一个单独的记录保存时,我设法做到了。我不能一次保存很多记录。
版本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' => ''
)
);
}
查看: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>
答案 0 :(得分:3)
阅读文档有帮助 http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#FormHelper::select
'multiple' => true
例如。
保存使用
saveMany()