私はこれに慣れていないので、愚かな間違いがあれば謝ります。ユーザーインデックスページに、各ユーザーが投稿した投稿の数を表示する列を持っています。列はありますが、ユーザーが投稿を行うたびに自動的に更新される方法を理解しようとしています。Cakephpディスプレイ番号。各ユーザーが作成した投稿の数
ユーザー/ index.ctp
<table>
<tr>
<th>Users</th>
<th>Account type</th>
<th>Registered</th>
<th>No. of posts</th>
</tr>
<?php foreach ($users as $user): ?>
<tr>
<td><?php echo $user['User']['username']; ?></td>
<td><?php echo $user['User']['role']; ?></td>
<td><?php echo $user['User']['created']; ?></td>
<td><?php echo $user['User']['post_count']; ?></td>
</tr>
<?php endforeach; ?>
</table>
私のデータベース内のユーザーテーブルのデフォルトの「POST_COUNTは」私の投稿コントローラにそう0
で、私はパートを追加しますポストを作るユーザのpost_countが1だけインクリメントされるadd()関数。add()関数は、PostsController.php:
public function add() {
if ($this->request->is('post')) {
$this->request->data['Post']['user_id'] = $this->Auth->user('id');
$this->request->data['Post']['user_username'] = $this->Auth->user('username');
$this->Post->create();
if ($this->Post->save($this->request->data)) {
$this->Flash->success(__('Your post has been saved.'));
return $this->redirect(array('action' => 'index'));
}
$this->Flash->error(__('Unable to add your post.'));
}
}
私は、次の行を追加してみました:
public function add() {
if ($this->request->is('post')) {
$this->request->data['Post']['user_id'] = $this->Auth->user('id');
$this->request->data['Post']['user_username'] = $this->Auth->user('username');
$this->Post->create();
if ($this->Post->save($this->request->data)) {
$this->Flash->success(__('Your post has been saved.'));
--> $postCount = $this->Auth->user('post_count');
--> $postCount++;
--> $user['post_count'] = $postcount;
return $this->redirect(array('action' => 'index'));
}
$this->Flash->error(__('Unable to add your post.'));
}
}
が、彼らは何の効果も持っていませんでした。 私が正しい道を歩いているかどうかを教えても、助けてくれれば助かります。
ありがとうございます!私はこれを実装しようとします。 –