我創建一個動態數組並保存到表這裏是我的儲蓄模式蛋糕PHP如何保存動態數組中的MySQL表
型號: 我有類別和種類有很多產品,然後產品有許多SUB產品和SUB產品可能有很多選擇
這裏是查看代碼,通過它我創建動態複選框
<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>
,這裏是我的控制器代碼
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;
}
}
提交後我得到這個數組
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'
)
)
)
)
)
,我想它保存在藝術細節表 ID
user_ID的
art_id CATEGORY_ID
PRODUCT_ID
subproduct_id
option_id
創建
![這是我的形式,通過它我很submiting Click Here to view image
是那些在屏幕底部的ArtDetails字段名?如果是這樣,您應該爲您的表單域一樣的模型領域的使'save'工作。此外,根據你們的關係結構中,如果您的ArtDetail只能有1個option_id,那麼你並不需要保存subproduct_id,也不是PRODUCT_ID。 – AgRizzo 2015-02-05 13:11:09