Rahul Saxena
Rahul Saxena

Reputation: 465

CakePhp multiple table data fetch on single page

I am new in cakePHP try to create a blog site where user add blog after category select, I have one categories table : fields: category_id, name and posts table : fields : id, category_id , title, body. I want to fetch all categories to a dropdown list. When user add new post they have to select category first then he is able to post anything..

My PostsController:

<?php 


class PostsController extends AppController{



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

    public function index(){

        $this->Paginator->setting = array(
            'limit' =>'10',
            'order' => array('Post.id' =>'Desc')
            /*'conditions' => array(
                'Post.user_id' => AuthComponent::user(id)
                )*/
            ); 

        $arrPosts = $this->Paginator->paginate('Post');

        $this->set('posts', $arrPosts);

    }

    public function view($id = null){

        if(!$id){

            throw new NotFoundException(__("Error Processing Request ID"));

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

            throw new NotFoundException(__("Error Processing Request POST"));

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

    public function add(){

         // HERE I want to fetch all categoryies from categories table and want to send to view

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

            $this->Post->create();
            if($this->Post->save($this->request->data)){
                $this->Session->setFlash(__('Blog Posted Sucessfully'));
                return $this->redirect(array('action' => 'index'));
            }else{
                $this->Session->setFlash(__('Unable to Post Blog '));
            }
        }


    }
}
?>

I want to show my category in add form:

Please help me ...

Upvotes: 0

Views: 831

Answers (2)

drmonkeyninja
drmonkeyninja

Reputation: 8540

In your controller action you need to use $this->set() to set a View variable. As long as you've got your associations setup correctly in your Post model you should then be able to use:-

$this->set('categories', $this->Post->Category->find('list'));

Cake's FormHelper should automatically know that the Post.category_id form field wants to be a select input with $categories as the options.

One further point, it would be better to set the view variables after processing the form as you won't need them if it saves correctly and so can reduce the number of database queries by 1.

If you use find('all') to retrieve the categories you will need to convert it into the format used by find('list') which can easily be done using Hash::combine():-

$categories = $this->Post->Category->find('all');
$this->set(
    'categories', 
    Hash::combine($categories, '{n}.Category.id', '{n}.Category.name')
);

The only real value in doing this is if you need $categories for something else.

Upvotes: 1

Ashutosh Singh
Ashutosh Singh

Reputation: 56

public function add(){

  /*Here get the category list & set the view (ctp file)*/
 $this->set('categories', $this->Post->Category->find('list'));

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

        $this->Post->create();
        if($this->Post->save($this->request->data)){
            $this->Session->setFlash(__('Blog Posted Sucessfully'));
            return $this->redirect(array('action' => 'index'));
        }else{
            $this->Session->setFlash(__('Unable to Post Blog '));
        }
    }

}

//Set The dropdown in ctp file
$this->Form->input('category_id', array('type'=>'select', 'options'=>'categories'));

Upvotes: 0

Related Questions