0

I am having three tables category, subcategory and subsubcategory. Now category table contain fields as id,name subcategory table contain fields as id, category_id and name and subsubcategory table contain fields as id, subcategory_id and name

now subsubcategory view/index showing columns as name,subcategory_name but i want to display category_name also in my subsubcategory index page

I have used containable behaviour to associate two tables as category and subsubcategory

Here is my code:--

subsubcategoriescontroller:

$contain =array(
'Subcategory'=> array(
    'Category' =>array(
        'fields' => array('id', 'name')
           )
     ));
     $this->Subsubcategory->find('all',array('contain' => $contain));

appmodel:

public $actsAs = array("Containable"); still my subsubcategory page not showing expected results. what will i do?

jessica
  • 11
  • 1
  • 6
  • What is your find result showing and what are you expecting? – BadHorsie Oct 22 '13 at 12:03
  • 1
    Specify the fields name in the `Subcategory` block inside the containable behavior. One more thing, you want to manage category at three level? if yes then you can manage it form one table categories(with parent_id). there is no need to created other tables for it. – Vikash Pathak Oct 22 '13 at 12:03
  • i have used fields section in subcategory block also but no result here is my code $contain =array( 'Subcategory'=> array( 'fields' => array('id', 'name') ), 'Subsubcategory' =>array( 'fields' => array('id', 'name') ) ); – jessica Oct 22 '13 at 12:07
  • On a side note, why all these tables/models? Why not using nested data? http://book.cakephp.org/2.0/en/models/retrieving-your-data.html#model-find-threaded | http://book.cakephp.org/2.0/en/core-libraries/behaviors/tree.html – ndm Oct 22 '13 at 12:11
  • add `'recursive' => 2` after contain with find condition. – Vikash Pathak Oct 22 '13 at 12:16
  • still no result here is i have changed $this->Subsubcategory->find('all',array('contain' => $contain,'recursive'=>2)); – jessica Oct 22 '13 at 12:26

1 Answers1

0

Have you got the correct relationships set up in your models?

class Category extends AppModel {

    public $hasMany = array('Subcategory');
}

class Subcategory extends AppModel {

    public $belongsTo = array('Category');
    public $hasMany = array('Subsubcategory');
}

class Subsubcategory extends AppModel {

    public $belongsTo = array('Subcategory');
}
BadHorsie
  • 14,135
  • 30
  • 117
  • 191
  • Yes BadHorsie, Here i got correct relationship set in my models same as above. can you please tell me how i show relationship between category and subsubcategory tables. i mean i want to fetch category name column in my subsubcategory index page but it is giving me an error as category model is not associated with subsubcategory – jessica Oct 22 '13 at 12:10