2017-01-06 19 views
0

CakePHPのvertionと削除に更新していません。CakePHPの3 CounterCacheはbelongsToMany SelfJoinTable

SentenceTable

$this->belongsToMany('Sentences', [ 
     'foreignKey' => 'second_sentence_id', 
     'targetForeignKey' => 'sentence_id', 
     'joinTable' => 'sentences_sentences' 
    ]); 
    $this->belongsToMany('SecondSentences', [ 
     'className' => 'Sentences', 
     'foreignKey' => 'sentence_id', 
     'targetForeignKey' => 'second_sentence_id', 
     'joinTable' => 'sentences_sentences' 
    ]); 

SentencesSentencesTable

$this->belongsTo('Sentences', [ 
     'foreignKey' => 'sentence_id', 
     'joinType' => 'INNER' 
    ]); 
    $this->belongsTo('SecondSentences', [ 
     'className'=>'Sentences', 
     'foreignKey' => 'second_sentence_id', 
     'joinType' => 'INNER' 
    ]); 

    $this->addBehavior('CounterCache', ['Sentences' => ['ver_count']]); 

$sentence = $this->Sentences->get($this->request->data['id']); 
$sentence = $this->Sentences->patchEntity($sentence, $this->request->data); 
      $this->Sentences->SecondSentences->saveStrategy('append'); 
      $this->Sentences->save($sentence); 

SentencesController

ver_count更新されない方法を削除ver_count SentencesController方法の更新を追加します。
$sentence = $this->Sentences->SecondSentences->get($this->request->data['id'],['contain'=>['Sentences']]); 
if ($sentence->user_id == $this->Auth->user('id')) { 
    $this->Sentences->SecondSentences->delete($sentence); 
    $sentences = $this->Sentences->get($sentence->sentences[0]->id,['contain'=>['SecondSentences']]); 

// NOW I AM USING BELOW CODE FOR UPDATING VER_COUNT. 
    $this->Sentences->updateAll(['ver_count'=>count($sentences->second_sentences)], ['id'=>$sentence->sentences[0]->id]); 
} 
+0

あなたの追加と削除の方法も投稿してください。だれかがあなたを助けることができます。 –

答えて

0

あなたの記録はどのように削除されますか? CakePHPのドキュメントに記載のと同じようにCounterCache

The counter will not be updated when you use deleteAll(), or execute SQL you have written. 

そして:

The CounterCache behavior works for belongsTo associations only. 

ただ、最初にそのことについて確認してください。

+0

はい私のfaultの関連付けは理由ですが、belgongsToManyの関連付けを伴うaddメソッドで動作します。 – Kani