Jump to content

CakePHP belongsTo, associations, need help getting data from another table


rxbanditboy1112

Recommended Posts

So I have a MVC for Events and Event Categories. I am trying to have the name of the category appear on the event page. I am associating events as belonging to event categories with a field called cat_id.

 

I am able the retrieve the tables, but for some reason the event category part of the array ends up empty.

 

Visually I am trying to make it look something like:

Concert: Annual Event

 

Where Concert is equal to a cat_name which is associated by cat_id(in the event table) = id(in the event categories table) and Annual Event is the event_title (in the event table).

 

Getting data from the event table is no problem.

 

I used print_r($event[0]) to show data from the first entry:

Array ( [Event] => Array ( [id] => 6 [cat_id] => 1 [gallery_id] => 6 [event_title] => Miggs, Your Last Chance, Kyle Reece, The Strategy, PK, Kinderghost [event_date] => 2008-02-09 [event_hour] => 6 [event_minute] => 0 [event_ampm] => 1 [event_price] => 10.00 [event_tickets] => 6 [event_venue] => San Jose Skate [event_address] => 397 Blossom Hill Rd. [event_city] => San Jose [event_state] => CA [event_zip] => 95123 [event_caption] => This Event Will be Will Call bring your ticket for faster service though. Oh the rock will be endless, and the merriment will be a plenty. [event_body] => This Event Will be Will Call bring your ticket for faster service though. Oh the rock will be endless, and the merriment will be a plenty. [event_author] => 1 [event_read_count] => 1339 [event_h_story] => 1 [event_capacity] => 0 [amount_sold] => 0 [event_enabled] => 1 ) [EventCategory] => Array ( [id] => [cat_name] => [cat_description] => ) ) 

 

If you notice the EventCategory part of the array is empty for some reason....

 

Event Model:

<?php
class Event extends AppModel
{
var $name = 'Event';
var $hasOne = array(
	'EventCategory' => array(
		'className'	=>	'EventCategory',
		'foreignKey' =>	'id'
		)
	);
}
?>

 

Event Category Model:

<?php
class EventCategory extends AppModel
{
var $name = 'EventCategory';
var $hasMany = array(
	'Event' => array(
		'className'	=>	'Recipe',
		'order'		=>	'Reciped
}
?>

 

Event Controller:

<?php
class EventController extends AppController
{
var $name = 'Event';

function index()
{
	$this->set('event', $this->Event->findAll());
}
}
?>

 

Event Category Controller:

<?php
class EventCategoryController extends AppController
{
var $name = 'EventCategory';

function index()
{
	$this->set('eventcategory', $this->EventCategory->findAll());
}
}
?>

 

Sorry, I know this is a bit lengthy, but I figured I would give a bunch of details rather than be vague. Hope someone can help.

yeaaaaa, i've been trying to find a good place to post. It seems that there is a limited amount of resources for cake. Sometimes no results for some issues... Especially for this question. I have been stuck for the past 2 or 3 days with this issue. Although, i think after this it should be smooth sailing. Its just not grabbing the data from the table. I see the table and column names in the array but the result is blank. I don't get it. It happens in all versions of cake of me so i know it isn't a distro problem.

 

I am gonna post else where also, but if anyone has used cake before here and knows what i am doing wrong let me know!! :)

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.