0

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') );

}
huzzah
  • 1,793
  • 2
  • 22
  • 40
  • categories.listing_id is wrong/unnecessary. Are you sure you have `public $belongsTo = array('Category');` in Listing model and `public $hasMany = array('Listing');` in `Category` model? – tigrang Sep 23 '12 at 04:48
  • categories.listing_id just got included accidentally when I was typing these arrays in (not actually in the database table, sorry!). The Listing Model and the Category model both have the correct syntax with belongsTo and hasMany. This is cake 1.3 so I am only using var $hasMany, etc, not public $hasMany. – huzzah Sep 23 '12 at 04:56
  • What's the output of `debug(get_class($this->Listing))`? Is it AppModel or Listing? Recursive should work, but trying Containable behavior wouldn't hurt. – tigrang Sep 23 '12 at 05:00
  • Well there's your problem. What's the file name, path (relative from your app directory), and class name for your Listing model – tigrang Sep 23 '12 at 05:08
  • It is /app/models/Listing.php and I had no class name declared in either Listing.php or Category.php – huzzah Sep 23 '12 at 05:14
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/17011/discussion-between-tigrang-and-huzzah) – tigrang Sep 23 '12 at 05:16

1 Answers1

2

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.

tigrang
  • 6,767
  • 1
  • 20
  • 22