Jump to content

how to display table with custom query in cakephp


ajeya

Recommended Posts

I have a 3-tables

 

1-> categories = id,category_name

2-> statuses = id,status_name

3-> evnets = id,event_name,category_id,staus_id

 

events is the main table which has the id`s of categories and statuses table

 

I want to display events display where the names of categories and statuses is displayed instead of name

 

it is possible through join, but what should to model, controller and view to give the desired output.

 

The table must display event_id, event_name,category_name,status_name

I did
class event extends AppModel
{

var $belongsTo = array(
    'Category' => array(
        'className' => 'Category',
        'foreignKey' => 'category_id',
        'conditions' => 'Event.category_id=Category.id',
        'fields' => '',
        'order' => ''
    ),
'Status'=>array(
'className' => 'Status',
        'foreignKey' => 'category_id',
        'conditions' => 'Event.category_id=Status.id',
        'fields' => '',
        'order' => ''
)

}
class EventsController extends AppController
{

function viewEvent()
{
$this->set('events',$this->Event->find('all'));
}
}

view
foreach($events as $event_arr) {
echo $event_arr['Category']['category_name'];
echo $event_arr['Status']['status_name'];
}

It is displaying all required but without match of category-id and status-id with events table

That is like this

event_id event_name category_name status_name
   1             aaa                  xxx                       enabled
   2             bbb                 yyy                        disabled
   3             ccc                  zzz                        

where the ans should be

event_id event_name category_name status_name
   1             aaa                  zzz                       enabled
   2             bbb                 yyy                       disabled
   3             ccc                  xxx                       enabled

please help what should I do

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.