【发布时间】:2015-02-05 12:49:03
【问题描述】:
我正在创建一个动态数组并保存到这里的表格中是我的保存模型
型号: 我有类别,类别有很多产品,然后产品有很多子产品,子产品可能有很多选项
这是我创建动态复选框的 VIEW 代码
<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
art_id
类别ID
产品编号
子产品ID
option_id
已创建
![这是我提交的表单 Click Here to view image
【问题讨论】:
-
是屏幕底部的 ArtDetails 字段名称吗?如果是这样,您应该将表单字段命名为与模型字段相同的名称,以使
save起作用。另外,根据你的关系结构,如果你的 ArtDetail 只能有 1 个 option_id,那么你不需要保存 subproduct_id,也不需要保存 product_id。