2016-11-23 10 views
0

私はCakePHPを初めてお使いになり、CakePHP cookbookとstackoverflowを使って問題を解決しようとしましたができませんでした。それはおそらく愚かなものです。CakePHP patchEntityがデータを保存していません( "through"アソシエーション)

私は、Quotes、Itemsとjointata QuotesItemsとのジョイントがあります。フォームを保存するときに送信される配列がある

public function edit($id = null) 
    { 
     $quote = $this->Quotes->get($id, [ 
      'contain' => ['Items'] 
     ]); 
     if ($this->request->is(['patch', 'post', 'put'])) { 
      $quote = $this->Quotes->patchEntity($quote, $this->request->data, ['Quotes._joinData']); 
      //$quote = $this->Quotes->patchEntity($quote, $this->request->data, ['associated'=>['QuotesItems'=>['associated'=>['Items']]]]); 
      if ($this->Quotes->save($quote)) { 
debug($this->request->data); 
       $this->Flash->success(__('The quote has been saved.')); 

       //return $this->redirect(['action' => 'index']); 
      } else { 
       $this->Flash->error(__('The quote could not be saved. Please, try again.')); 
      } 
     } 
     $customers = $this->Quotes->Customers->find('list', ['limit' => 200]); 
     $items = $this->Quotes->Items->find('list', ['limit' => 200]); 
     $this->set(compact('quote', 'customers', 'items')); 
     $this->set('_serialize', ['quote']); 
    } 

:私は編集機能を持っているQuotesControllerで

<div class="quotes form large-9 medium-8 columns content"> 
    <?= $this->Form->create($quote) ?> 
    <fieldset> 
     <legend><?= __('Edit Quote') ?></legend> 
     <?php 
      echo $this->Form->input('customer_id', ['options' => $customers]); 
      echo $this->Form->input('items._ids', ['options' => $items]); 
     $counter = 0; 
     foreach($quote['items'] as $item): 
      echo $this->Form->input("items.$counter.description", array('disabled' => 'disabled')); 
      echo $this->Form->input("items.$counter._joinData.item_quantity"); 
      $counter++; 
     endforeach 
     ?> 
    </fieldset> 
    <?= $this->Form->button(__('Submit')) ?> 
    <?= $this->Form->end() ?> 
</div> 

私は(テンプレート引用)私のedit.ctpでこれを持っています:

[ 
    'customer_id' => '1', 
    'items' => [ 
     '_ids' => [ 
      (int) 0 => '1' 
     ], 
     (int) 0 => [ 
      '_joinData' => [ 
       'item_quantity' => '18' 
      ] 
     ] 
    ] 
] 

私は料理本の「保存データ」をチェックしましたが、間違っていることを明確にしていません。

データには見積もりIDが必要ですか?私はアイテムを正しく反復しているのですか? 引用::

$this->belongsToMany('Items', [ 
     'through' => 'QuotesItems' 
    ]); 

アイテム:

$this->belongsToMany('Quotes', [ 
     'through' => 'QuotesItems' 
    ]); 

QuotesItems(jointable):

$this->belongsTo('Quotes', [ 
     'foreignKey' => 'quote_id', 
     'joinType' => 'INNER' 
    ]); 
    $this->belongsTo('Items', [ 
     'foreignKey' => 'item_id', 
     'joinType' => 'INNER' 
    ]); 

感謝を事前に次のように

表の関連付けが行われています!

答えて

0

このような配列形式に何か作ってみましょう:

$quote = $this->Quotes->patchEntity($quote, $this->request->data, ['associated'=>['QuotesItems'=>['associated'=>['Items']]]]); 
+0

ご回答をありがとう:

[ 'customer_id' => '1', 'items' => [ 'id' => 1, // this is primary key of items table 'item_quantity' => '18' // this is another field of items table ] ] 

そして、あなたの編集方法の使用では、行にコメントしています。私があなたがコントローラに言及した線を置くと、「QuoteItems」関連のデータをマーシャリングできないという例外が発生します。それは "引用"と関連していません。それ以外は、cakePhpがcake bakeコマンドを使ってこのような "基本的な"シナリオを扱うことができず、データを修正するために私が払わなければならないことに驚いています(別のシナリオでは理解できますが、これは基本的です)。 cakephpには多くの文書がありますが、これは実際にはっきりと説明されていません。 – th3penguinwhisperer

+0

忘れてしまったこと:アイテムのすぐ下にIDがあることがわかりました。そしてitem_quantityも同様です。 _joinDataにネストされていないはずですか?私はジョイント可能な主キーを持っていますので、データにもそれが必要であると思いますか?前もって感謝します。 – th3penguinwhisperer

+0

テーブルが_joinDataにネストされていない適切な関係にある場合はどうでしょうか。私は前にそれをチェックし、うまくいきます。 –

関連する問題