douqianmin5367 2011-10-29 07:47
浏览 46
已采纳

Cakephp通过find选项无法正常工作

I'm trying to join a Users table to my curent hasMany Through table which has Interest and User model id's.

Below is the find query with options:

$params = array(
                    'fields' => array('*', 'COUNT(DISTINCT(InterestsUser.interest_id)) as interest_count'),
                    'limit' => 15,
                    'recursive' => -1,
                    'offset' => $offset,
                    'conditions' => array('InterestsUser.interest_id' => $conditions),
                    'group' => array('InterestsUser.user_id'),
                    'order' => array('interest_count DESC', 'InterestsUser.user_id ASC', 'InterestsUser.interest_id ASC'),
                        'joins' => array(
                            array('table' => 'users',
                                'alias' => 'User',
                                'type' => 'LEFT',
                                'conditions' => array(
                                    'User.id' => 'InterestsUser.user_id',
                                )
                            )
                        )
                    );

            $results = $this->InterestsUser->find('all', $params);

This returns InterestsUser table fine but does not return any values for Users table. It only returns field names.

What could be wrong?

UPDATE: OK, above is generating below SQL which I got from Cake's datasources sql log:

SELECT *, COUNT(DISTINCT(InterestsUser.interest_id)) as interest_count 
FROM `interests_users` AS `InterestsUser` 
LEFT JOIN users AS `User` ON (`User`.`id` = 'InterestsUser.user_id')  
WHERE `InterestsUser`.`interest_id` IN (3, 2, 1)  
GROUP BY `InterestsUser`.`user_id`  
ORDER BY `interest_count` DESC, `InterestsUser`.`user_id` ASC, `InterestsUser`.`interest_id` ASC  
LIMIT 15

Why is users table values returning NULL only for all fields?

UPDATE:

OK I tried below but this is working fine...What am I missing here!!??

SELECT * , COUNT( DISTINCT (
interests_users.interest_id
) ) AS interest_count
FROM interests_users
LEFT JOIN users ON ( users.id = interests_users.user_id ) 
WHERE interests_users.interest_id
IN ( 1, 2, 3 ) 
GROUP BY interests_users.user_id
ORDER BY interest_count DESC 
LIMIT 15
  • 写回答

1条回答 默认 最新

  • dth8312 2011-10-29 09:25
    关注

    The array syntax for join conditions should be like the following

    array('User.id = InterestsUser.user_id')
    

    as opposed to array('User.id' => 'InterestsUser.user_id'). For more, see http://book.cakephp.org/view/1047/Joining-tables.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题