我正在创建一个动态数组,保存到表中这是我的保存模型
型号: 我有类别和类别有很多产品,然后产品有很多子产品和子产品可能有很多选项
这是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>
这是我的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->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
创建了
![这是我提交的表格 Click Here to view image
答案 0 :(得分:0)
为什么不通过序列化数组存储数据? 只需序列化($ yourarr);并使用您想要识别数组的唯一键存储
答案 1 :(得分:0)
array(
'Artproject' => array(
'ainum' => '1024',
...
'Artdetail' => array(...)
),
)
将Artdetail对象放入Artproject中。 然后,您只需在控制器中使用此代码:
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))) { }
}
确保您的模型关联正确无误。
答案 2 :(得分:0)
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' => ''
),
);