[英]CAKE-PHP how to save dynamic array in the MYSQL table
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 这是我用来创建动态复选框的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>
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 我想将其保存在艺术品详细信息表ID中
user_id 用户身份
art_id category_id art_id category_id
product_id PRODUCT_ID
subproduct_id subproduct_id
option_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); 只需序列化($ 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. 将您的Artdetail对象放入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: ARTPROJECT模型:
public $hasMany = array(
'Artdetail' => array(
'className' => 'Artdetail',
'foreignKey' => 'artproject_id'
)
);
public $belongsTo = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'user_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.