2015-02-05 66 views
0

我創建一個動態數組並保存到表這裏是我的儲蓄模式蛋糕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

+0

是那些在屏幕底部的ArtDetails字段名?如果是這樣,您應該爲您的表單域一樣的模型領域的使'save'工作。此外,根據你們的關係結構中,如果您的ArtDetail只能有1個option_id,那麼你並不需要保存subproduct_id,也不是PRODUCT_ID。 – AgRizzo 2015-02-05 13:11:09

回答

0

爲什麼不通過序列化陣列存儲數據? 簡單的序列化(yourarr $);與要確定您的陣列

+0

感謝答覆它的保存在表中,但是當我選擇選項時,它的創建混亂並且不保存option_id列中的選項 – 2015-02-05 13:16:14

+0

將兩列之一作爲option_id,它是主鍵和自動增量。其次是數據類型爲text的數據,並將序列化的數據存儲在文本列中。但我會建議使用主鍵作爲一些價值,這使得你很容易遍歷,例如:ainum和product id複合鍵 – 2015-02-06 09:57:29

+0

我不會將它保存爲序列化數組....你不能真正在數據庫中搜索你的值了。或者至少這是a ** e中的痛苦。 而是學習如何CakePHP中真正起作用的是 – conehead 2015-02-06 13:05:06

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

    } 

只要確保你的模型協會是正確的。

+0

感謝答覆你能告訴我有關協會,這將是我的模型關聯 – 2015-02-06 11:51:17

+0

陣列( \t「ainum」 =>「AI- 1024' , \t 'teamname'=> '籃球', \t 'USER_ID'=> '10', \t 'Artdetail'=>數組( \t \t '產品'=>數組( \t \t \t(INT )0 =>'1', \t \t \t(INT)1 =>數組( \t \t \t \t '子產品'=>數組( \t \t \t \t \t(INT)0 => '1', \t \t \t \t \t(INT)1 =>數組( \t \t \t \t \t \t '選項'=>數組( \t \t \t \t \t \t \t(INT)0 => '1', \t \t \t \t \t \t \t(INT)1 => '2' \t \t \t \t \t \t) \t \t \t \t \t) \t \t \t \t \t (int)2 =>'2', \t \t \t \t \t(INT)3 => '3' \t \t \t \t) \t \t \t) \t \t) \t) \t 'createdby'=> '7' 現在,這是Puting的Artdetail後我的數組我的藝術項目中的對象。 – 2015-02-06 12:00:52

+0

請顯示您的模型的一些代碼。 猜你需要的東西類似: 產品HABTM分類 產品HABTM產品(子產品) 產品HABTM選項。 好吧...取決於你想要什麼。它可能只是一個belongsTo或hasMany關係。 – conehead 2015-02-06 12:56:01

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' => '' 
     ), 

    ); 
+0

嗯莫名其妙這看起來有點奇怪。 Artdetail可能與Artproject有不同的用戶嗎? 如果不是,爲什麼Artdetail也有用戶? 每個Artdetail只能有一個類別,只有一個產品,只有一個子產品? (順便說一下......我不認爲ArtDetail'屬於'產品....不是'有'嗎?) 是子產品本身?你的選擇是什麼? 說實話,我並沒有真正明白你實際想要達到什麼;) 在我看來,像一個小商店系統。 如果是這種情況,那麼你應該找一個不同的模式。 – conehead 2015-02-06 14:49:35