2

I have a problem with nested model pagination using containable in Cake...

I've got three models: Category, CompanyCategory, Company and there association is like this

Category hasMany CompanyCategory
CompanyCategory belongsTo Category 
CompanyCategory belongsTo Company
Company hasMany CompanyCategory

I get data using contain, as shown below:

$options = [
  'conditions' => ['Category.slug' => $slug],
  'contain' => [
    'CompanyCategory.Company.CompanyAddress'
  ]
];
return $this->find('first', $options);

Everything works properly, until I wanted to paginate the nested model - Company. I use CategoriesController with show method, which renders the view with the selected category and associated companies (Company hasMany Categories and Category hasMany Companies).

I've tried something like this:

$this->Paginator->settings = [
  'limit' => 1,
  'order' => [
    'id' => 'asc'
  ],
  'contain' => array('CompanyCategory.Company')
];
$data = $this->Paginator->paginate('Category.CompanyCategory.Company', array('Category.slug LIKE' => $slug));
$this->set('category', $data);

But this didn't work for me :(

Any suggestions / help?

Moyed Ansari
  • 8,436
  • 2
  • 36
  • 57
kris
  • 23
  • 1
  • 5
  • What do you mean by nested model ? What is the relationship btw models ? – Moyed Ansari Apr 15 '14 at 07:30
  • I mean nested as associated. Category hasManyCompanyCategory (which is linking table between Category and Company), CompanyCategory belongsTo Category and belongsTo Company. Company hasMany CompanyCategory. This works fine, but I don't know how to implement pagination for Companies. – kris Apr 15 '14 at 07:32
  • why dont you make a relationship of Companies HABTM Category. this can solve your problem – Moyed Ansari Apr 15 '14 at 07:43
  • I tried with HABTM, but still got paginated Categories not Companies as in the following example – kris Apr 15 '14 at 08:25
  • you want to paginate Companies, right ? – Moyed Ansari Apr 15 '14 at 08:32
  • yeah, I want to paginate Companies – kris Apr 15 '14 at 08:43

2 Answers2

1

If you define Category has a hasAndBelongsToMany relation, it would be easy for you. Thus you created a new Model called CompanyCategory, i assume there is a reason..

You can try this-

$this->Category->bindModel(
    array(
        'hasAndBelongsToMany' => array(
            'Company'
        )
    )
);
$this->Paginator->settings = array(
    'conditions' => array(
        'Category.slug' => $slug
    ),
    'limit' => 1,
    'order' => array(
        'id' => 'asc'
    ),
    'contain' => array(
            'Company'
    )
);

I assume your relation table name is categorys_companies.... If not then you need take different approach

Moyed Ansari
  • 8,436
  • 2
  • 36
  • 57
Fazal Rasel
  • 4,446
  • 2
  • 20
  • 31
0

Finnaly I resolve my problem using

$this->Paginator->settings = array(
  'limit' => 2,

);

$this->Paginator->paginate('Category.CompanyCategory.Company');

I forgot to write that I use method from CategoriesController, not Companies.

But now another problem blocked me... I use TranslateBehaviour, and data which I get has no translations...

Any idea? I googled a lot, but couldn't find any satisfactory solution.

kris
  • 23
  • 1
  • 5