2017-01-06 6 views
0

의 CakePHP vertion과 삭제에 업데이트되지 : 3.3.11CakePHP의 3 CounterCache는 belongsToMany SelfJoinTable

CounterCache이 추가 방법 작업 만 삭제 방법에 작동하지 않습니다.

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

예 내 잘못 연결은 이유가 있지만 belgongsToMany 연결과 함께 add 메소드에서 작동합니다. – Kani