huzzah
huzzah

Reputation: 1805

retrieving the 'belongsTo' part of model data in cakephp

Maybe it is because I am dead tired but I am completely missing something here. I inherited a cake 1.3 app from a friend who set his tables up correctly but I am missing some data. When I print the data array what I expect is this:

 Array
(
[Listing] => Array
    (
        [id] => 74
        [listing_name] => El Toro
        [category_id] => 3
        [location_id] => 2
        [long_desc] => 
    ),

[Category] => Array
    (
        [id] => 3
        [category_name] => Restaurant

    ),
)

(Listing belongsTo Category). The controller function is defined like this:

function view_detail(){

    $this->set('um','true');
    $l=$this->Listing->find('first', array('conditions'=>array('id'=>$this->params['cid'])));
    $this->set('lst', $l);


}

Instead I get this:

 Array
(
[Listing] => Array
    (
        [id] => 74
        [listing_name] => El Toro
        [category_id] => 3
        [location_id] => 2
        [long_desc] => 
    )
)

Looking through the database tables he set up, all of cake's conventions have been followed: listings has category_id and categories has listing_id. I believe I was told a while back that if you have simple cookie cutter models/associations you don't even need to set up model pages as cake will automagically know the associations. But I did it anyways and it didn't make a difference. I tried setting recursive all the way up to 2 and it made no difference. Sooooo....not being good at cakephp, I have no idea what I'm not doing right. I'm looking to list the category name(Restaurant) of the listing, by the way.

UPDATE

Here are my 2 models:

class Category extends AppModel{

var $hasMany = array( 'Listing' );

}


class Listing extends AppModel{
var $name='Listing';
var $belongsTo = array( 'Location'=>array('foreignKey'=>'location_id'),   'Category'=>array('foreignKey'=>'category_id') );

}

Upvotes: 0

Views: 909

Answers (1)

tigrang
tigrang

Reputation: 6767

In 1.x model files should be lower case and underscored, so app/model/listing.php. When your model class shows as AppModel it means cake isn't finding your model class and making a generic one based off of AppModel.

Upvotes: 2

Related Questions