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