Cakephp 显示编号每个用户发表的帖子
Cakephp display no. of posts made by each user
我是新手,对于任何愚蠢的错误深表歉意。我想在用户索引页面上有一列,显示每个用户制作了多少 post。该专栏在那里,但我想弄清楚如何让它在用户进行 post.
时自动更新
users/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>
我的数据库中用户table中默认的'post_count'是0。
所以在我的 Posts 控制器中,我想向 add() 函数添加一个部分,其中使 post 的用户的 post_count 递增 1。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.'));
}
}
但它们没有效果。
任何帮助将不胜感激,即使只是告诉我我是否在正确的轨道上。
CakePHP 有一个内置函数 (CounterCache) 用于您要执行的操作:http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html#countercache-cache-your-count
好吧,在用户 table 中增加或减少 post 计数是错误的方法。解决这个问题的最好方法是在 User & Post 模型中定义 hasMany & belongsTo 关系。一旦关系适合与外键。在用户索引页面中,您将获得属于每个用户的 post 的数量。
注意:获取用户
创建的 post 的数量
以后有类似问题的朋友,我用counterCache函数解决了。
我在Post模型中写道:
public $belongsTo = array(
'User' => array(
'counterCache' => true,
)
);
我是新手,对于任何愚蠢的错误深表歉意。我想在用户索引页面上有一列,显示每个用户制作了多少 post。该专栏在那里,但我想弄清楚如何让它在用户进行 post.
时自动更新users/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>
我的数据库中用户table中默认的'post_count'是0。
所以在我的 Posts 控制器中,我想向 add() 函数添加一个部分,其中使 post 的用户的 post_count 递增 1。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.'));
}
}
但它们没有效果。 任何帮助将不胜感激,即使只是告诉我我是否在正确的轨道上。
CakePHP 有一个内置函数 (CounterCache) 用于您要执行的操作:http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html#countercache-cache-your-count
好吧,在用户 table 中增加或减少 post 计数是错误的方法。解决这个问题的最好方法是在 User & Post 模型中定义 hasMany & belongsTo 关系。一旦关系适合与外键。在用户索引页面中,您将获得属于每个用户的 post 的数量。
注意:获取用户
创建的 post 的数量以后有类似问题的朋友,我用counterCache函数解决了。
我在Post模型中写道:
public $belongsTo = array(
'User' => array(
'counterCache' => true,
)
);