Hi,

I want to sort a column based on HABTM associations using pagination.
Tables:
1) problems(id, title)
2) users(id, username)
3) problems_users(problem_id, user_id)

Relation ship between problems and users table is HABTM

 var $hasAndBelongsToMany = array(
                       'User' => array(                                         
                                                                                
                                'className' =>
'User',                                                                         
                                                                'joinTable' =>
'problems_users',                                                               
                                                                'foreignKey' =>
'problem_id',                                                                   
                                                                
'associationForeignKey' =>
'user_id'                                                                       
                                                         )
  );

I am getting pagination data like below:
Array
(
    [0] => Array
        (
            [Problem] => Array
                (
                    [id] => 5
                    [title] => 33
               )
            [User] => Array
                (
                    [0] => Array
                        (
                            [id] => 1
                            [username] => business user
                            [ProblemsUser] => Array
                                (
                                    [problem_id] => 5
                                    [user_id] => 1
                                ) )))

I am successfully able to sort on problem title column using <?php
echo $paginator->sort('Title', 'Problem.title'); ?>
but when I am trying to sort on username using <?php echo $paginator-
>sort('Username', User.username'); ?> its generating errors:

Warning (512): SQL Error: 1054: Unknown column 'User.username' in
'order clause' [CORE\cake\libs\model\datasources\dbo_source.php, line
521]

Query: SELECT `Problem`.`id`, `Problem`.`slug`, `Problem`.`title`,
`Problem`.`description`, `Problem`.`business_id`,
`Problem`.`reviewed`, `Problem`.`replied`, `Problem`.`status`,
`Problem`.`created` FROM `problems` AS `Problem`   WHERE
`Problem`.`business_id` = 2   ORDER BY `User`.`username` asc  LIMIT 5


How can I sort a column (username) based on above HABTM associations?

Please help me.

Thanks!!

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to