蛋糕php博客示例与auth登录页面这里index()方法我必须显示自己的帖子其他帖子不应该显示..这里我改变但它显示错误所以改为基本可以任何人帮助我
控制器/ PatientslistController.php
<?php
class PatientslistController extends AppController {
public $helpers = array('Html', 'Form', 'Session');
public $components = array('Session');
public function index() {
$this->set('posts', $this->Patientslist->find('all'));
}
public function view($id) {
if (!$id) {
throw new NotFoundException(__('Invalid post'));
}
$post = $this->Patientslist->findById($id);
if (!$post) {
throw new NotFoundException(__('Invalid post'));
}
$this->set('post', $post);
}
public function add() {
if ($this->request->is('post')) {
//Added this line
$this->request->data['Patientslist']['user_id'] = $this->Auth->user('id');
if ($this->Patientslist->save($this->request->data)) {
$this->Session->setFlash(__('Your post has been saved.'));
return $this->redirect(array('action' => 'index'));
}
}
}
/* public function add() {
if ($this->request->is('post')) {
$this->Post->create();
if ($this->Post->save($this->request->data)) {
$this->Session->setFlash(__('Your post has been saved.'));
return $this->redirect(array('action' => 'index'));
}
$this->Session->setFlash(__('Unable to add your post.'));
}
}
*/
public function edit($id = null) {
if (!$id) {
throw new NotFoundException(__('Invalid post'));
}
$post = $this->Patientslist->findById($id);
if (!$post) {
throw new NotFoundException(__('Invalid post'));
}
if ($this->request->is(array('Patientslist', 'put'))) {
$this->Patientslist->id = $id;
if ($this->Patientslist->save($this->request->data)) {
$this->Session->setFlash(__('Your post has been updated.'));
return $this->redirect(array('action' => 'index'));
}
$this->Session->setFlash(__('Unable to update your post.'));
}
if (!$this->request->data) {
$this->request->data = $post;
}
}
public function delete($id) {
if ($this->request->is('get')) {
throw new MethodNotAllowedException();
}
if ($this->Patientslist->delete($id)) {
$this->Session->setFlash(
__('The post with id: %s has been deleted.', h($id))
);
return $this->redirect(array('action' => 'index'));
}
}
public function isAuthorized($user) {
// All registered users can add posts
if ($this->action === 'add') {
return true;
}
// The owner of a post can edit and delete it
if (in_array($this->action, array('edit', 'delete'))) {
$postId = $this->request->params['pass'][0];
if ($this->Patientslist->isOwnedBy($postId, $user['id'])) {
return true;
}
}
return parent::isAuthorized($user);
}
}
?>
模型/ Patientslist.php
<?php class Patientslist extends AppModel {
public function isOwnedBy($post, $user) {
return $this->field('id', array('id' => $post, 'user_id' => $user)) === $post;
}
}
?>
查看/ Patientslist / index.ctp
<h1>Blog posts</h1>
<p><?php echo $this->Html->link('Add Post', array('action' => 'add')); ?></p>
<table>
<tr>
<th>Id</th>
<th>Title</th>
<th>Actions</th>
<th>Created</th>
</tr>
<!-- Here's where we loop through our $posts array, printing out post info -->
<?php foreach ($posts as $post): ?>
<tr>
<td><?php echo $post['Patientslist']['id']; ?></td>
<td>
<?php
echo $this->Html->link(
$post['Patientslist']['title'],
array('action' => 'view', $post['Patientslist']['id'])
);
?>
</td>
<td>
<?php
echo $this->Form->postLink(
'Delete',
array('action' => 'delete', $post['Patientslist']['id']),
array('confirm' => 'Are you sure?')
);
?>
<?php
echo $this->Html->link(
'Edit', array('action' => 'edit', $post['Patientslist']['id'])
);
?>
</td>
<td>
<?php echo $post['Patientslist']['created']; ?>
</td>
</tr>
<?php endforeach; ?>
</table>
答案 0 :(得分:1)
public function index() {
$this->set('posts', $this->Patientslist->find('all', array(
'conditions' => array(
'Patientslist.user_id' => $this->Auth->user('id')));
}
使用PatientLists表中的相关用户字段更改“user_id”。