i am creating a dynamic array and saving into the table here is my saving model
Model: i have CATEGORIES and CATEGORIES have many PRODUCTS and then PRODUCTS have many SUB PRODUCTS and SUB PRODUCTS may have many OPTIONS
Here is VIEW code through which i am creating dynamic check boxes
<tr>
<td style="background-color:#f3f5f6;">
<?php // debug($Product);exit; ?>
<?php
//foreach ($Product as $Products): ?>
<?php echo $this->Form->checkbox('Artdetail.products.', array('value'=>$Product['Product']['id'],'hiddenField'=>false)); ?>
<?php echo $Product['Product']['product_name'].'<br/>' ?>
<?php
foreach ($Product['SubProduct'] as $subproducts):?>
<?php echo $this->Form->checkbox('Artdetail.products.'.$Product['Product']['id'].'.subproducts.', array('value'=>$subproducts['id'],'hiddenField'=>false)); ?>
<?php echo $subproducts['subproduct_name'].'<br/>' ?>
<?php
foreach ($subproducts['Option'] as $options): ?>
<?php echo $this->Form->checkbox('Artdetail.products.'.$Product['Product']['id'].'.subproducts.'.$subproducts['id'].'.options.', array('value'=>$options['id'],'hiddenField'=>false)); ?>
<?php echo $options['optname'].'<br/>' ?>
<?php
endforeach;?>
<?php
endforeach; ?>
<?php endforeach;?>
</td>
</tr>
and here is my CONTROLLER code
if ($this->request->is('post')) {
$this->Artproject->create();
$this->request->data['Artproject']['ainum'] = 'AI-' . $this->request->data['Artproject']['ainum'];
$this->request->data['Artproject']['createdby'] = $this->Auth->user('id');
if ($this->Artproject->save($this->request->data['Artproject'])) {
$artid = $this->Artproject->getLastInsertID();
foreach ($this->request->data['Artdetail']['products'][$prdid]['subproducts'] as $subproducts):
//debug($subproducts);
$this->request->data['Artdetaill']['category_id'] = $sportid;
$this->request->data['Artdetaill']['product_id'] = $prdid;
$this->request->data['Artdetaill']['subproduct_id'] = $subproducts;
$this->request->data['Artdetaill']['user_id'] = $this->request->data['Artproject']['user_id'];
$this->request->data['Artdetaill']['art_id'] = $artid;
if(!empty($subproducts['options'])){
foreach ($subproducts['options'] as $options):
$this->request->data['Artdetaill']['option_id'] = $options;
$this->Artdetail->saveAll($this->request->data['Artdetaill']);
endforeach;
}else{
$this->Artdetail->saveAll($this->request->data['Artdetaill']);
}
endforeach;
exit;
}
}
after submit i get this array
array(
'Artproject' => array(
'ainum' => '1024',
'teamname' => 'Basketball',
'user_id' => '10',
'createdby' => ''
),
'Artdetail' => array(
'products' => array(
(int) 0 => '1',
(int) 1 => array(
'subproducts' => array(
(int) 0 => '1',
(int) 1 => array(
'options' => array(
(int) 0 => '1',
(int) 1 => '2'
)
),
(int) 2 => '2',
(int) 3 => '3'
)
)
)
)
)
and i want to save it in the art details table id
user_id
art_id category_id
product_id
subproduct_id
option_id
created
![This is my form through which i am submiting Click Here to view image
Why not store data by serializing array? Simply serialize($yourarr); and store with a unique key you want to identify your array
Wow....way too much code.
array(
'Artproject' => array(
'ainum' => '1024',
...
'Artdetail' => array(...)
),
)
Put your Artdetail object within your Artproject. Then you only need this code in your controller:
if ($this->request->is('post')) {
$this->Artproject->create();
$this->request->data['Artproject']['ainum'] = 'AI-' . $this->request->data['Artproject']['ainum'];
$this->request->data['Artproject']['createdby'] = $this->Auth->user('id');
if ($this->Artproject->saveAssociated($this->request->data['Artproject'], array('deep' => true))) { }
}
Just make sure your model-associations are correct.
ARTDETAIL MODEL:
public $belongsTo = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'user_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'Artproject' => array(
'className' => 'Artproject',
'foreignKey' => 'artproject_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'Category' => array(
'className' => 'Category',
'foreignKey' => 'category_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'Product' => array(
'className' => 'Product',
'foreignKey' => 'product_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'Subproduct' => array(
'className' => 'Subproduct',
'foreignKey' => 'subproduct_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'Option' => array(
'className' => 'Option',
'foreignKey' => 'option_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
ARTPROJECT MODEL:
public $hasMany = array(
'Artdetail' => array(
'className' => 'Artdetail',
'foreignKey' => 'artproject_id'
)
);
public $belongsTo = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'user_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.