Cakephp receives a HABTM whose terms

I use cakephp and want to display all Views that fall into the category "X". I have 4 tables with a HABTM relationship.

Users → (hasMany) → Views ↔ (hasAndBelongsToMany) ↔ Categories

but I would like to do this using $ this-> paginate (), and for each view I would like to display the user who posted the message.

User table

Id   |      Name         
-----+-------------------
1    |   User 1      
2    |   User 2     

Submission table

Id   |      Name         |   User_id
-----+-------------------+--------------
1    |   Submission 1    |      1    
2    |   Submission 2    |      2    

Category table

Id   |      Name 
-----+-------------------
1    |   Category 1        
2    |   Category 2        

Subcategory Table

Id   |   Submission_id   |   Category_id 
-----+-------------------+-------------------
1    |         1         |        1     
2    |         1         |        2  
3    |         2         |        1      

I am really having trouble creating a paginate that can do this, I am starting to think that this is not possible if I do not miss something.

If I have not used cakephp, this is the request I would like to make

SELECT 
     * 
FROM 
     submissions_categories, 
     submissions, 
     users
WHERE 
     submissions_categories.category_id = 8 
          AND 
     submissions_categories.submission_id = submissions.id 
          AND 
     submissions.user_id = users.id
+5
1

HABTM CakePHP, . , $paginate. paginate(). :

<?php
class SubmissionsController extends AppController {

 var $name = 'Submissions';
 var $helpers = array('Html', 'Form');
 var $paginate = array('joins' => array(
     array( 
               'table' => 'submissions_categories', 
               'alias' => 'SubmissionsCategory', 
               'type' => 'inner',  
               'conditions'=> array('SubmissionsCategory.submission_id = Submission.id') 
           ), 
           array( 
               'table' => 'categories', 
               'alias' => 'Category', 
               'type' => 'inner',  
               'conditions'=> array( 
                   'Category.id = SubmissionsCategory.category_id'
               ) 
           )));

 function index() {
  $this->Submission->recursion = 1;
  $this->set('submissions', $this->paginate(array('Category.id'=>1)));
 }
}

?>
+7

All Articles