簡體   English   中英

CAKE-PHP如何在MYSQL表中保存動態數組

[英]CAKE-PHP how to save dynamic array in the MYSQL table

我正在創建一個動態數組並將其保存到表中,這是我的保存模型

型號:我有類別,並且類別有很多產品,然后產品有很多子產品,子產品可能有很多選擇

這是我用來創建動態復選框的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 category_id
PRODUCT_ID
subproduct_id
option_id
創建

![這是我提交的表單,請點擊此處查看圖像

為什么不通過序列化數組存儲數據? 只需序列化($ yourarr); 並使用要標識陣列的唯一密鑰進行存儲

哇...代碼太多。

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))) { }

    }

只要確保您的模型關聯正確即可。

  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模型:

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM