Fetch for users only

I am new to CAKEPHP and want to receive a message that is sent by me or just for login. How do I get these messages? I have no idea.

This is the post controller code:

<?php 
class PostsController extends AppController {

  public $helpers = array('Html', 'Form', 'Session');
  public $components = array('Session','Paginator');

  public $paginate = array(
        'limit' => 10,
        'order' => array(
            'Post.id' => 'desc'
        )
    );

public function index(){

 $this->Paginator->settings = $this->paginate;

// Pagination Code Limit define to top

    // similar to findAll(), but fetches paged results
    $data = $this->Paginator->paginate();
    $this->set('posts', $data);

}



public function add(){

if($this->request->is('post')){

$this->Post->create();
  $this->request->data['Post']['user_id'] = $this->Auth->user('id');
if($this->Post->save($this->request->data)){


    $this->Session->setFlash(__('Your Post is saved!!'));
    return $this->redirect(array('action' => 'index' ));
}
$this->Session->setFlash(__('Unable to post data.'));

}

}


public function edit($id = null) {
    if (!$id) {
        throw new NotFoundException(__('Invalid post'));
    }

    $post = $this->Post->findById($id);
    if (!$post) {
        throw new NotFoundException(__('Invalid post'));
    }

    if ($this->request->is(array('post', 'put'))) {
        $this->Post->id = $id;
        if ($this->Post->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 view($id = null){

if(!$id){

  throw new NotFoundException(__('Invalid Post'));

}

$post = $this->Post->findById($id);
if(!$post){

 throw new NotFoundException(__('Invalid Post'));

}

$this->set('post',$post);

}
public function delete($id){

if($this->request->is('get')){


    throw new MethodNotAllowedException();
}

if($this->Post->delete($id)){

$this->Session->setFlash(__('Post Deleted Sucessfully!!'));

}else{

    $this->Session->setFlash(
            __('The post with id: %s could not be deleted.')
        );
}

return $this->redirect(array('action' => 'index'));

}


}
?>

      

And my model:

<?php 


class Post extends AppModel {
   public  $validate = array(
        'title' => array(
            'rule'=> 'notEmpty'
            ),
        'body' => array(
            'rule'=> 'notEmpty'
            )

        );

}

?>

      

I want to receive messages that are posted by me or by a user who is a login and not full data. Please help me. Thank!

+3


source to share


2 answers


Add conditions when you fetch posts

-

$condition = array(
 'conditions' => array('posts.user_id' => $this->Auth->user('id')),
)

$this->Paginator->settings = array(
    'conditions' => $conditions,
);

      



Cakephp pagination

0


source


You need to make the following changes to your controller in order to receive your and registered user posts

First of all add the Auth component

public $components = array('Session','Paginator','Auth');

      

Second, enter your user id



$your_id (This is your user Id)

      

Finally, your paginator assignment

public $paginate = array(
    'limit' => 10,
    'conditions' => array('posts.user_id' => $this->Auth->user('id'),'posts.user_id' => $your_id),
    'order' => array(
        'Post.id' => 'desc'
    )
);

      

0


source







All Articles