gpt4 book ai didi

php - Cakephp 通过查找选项连接表不起作用

转载 作者:行者123 更新时间:2023-11-29 00:52:18 25 4
gpt4 key购买 nike

我正在尝试将 Users 表连接到我当前的 hasMany Through 表,该表具有 Interest 和 User 模型 ID。

下面是带有选项的查找查询:

$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);

这会正常返回 InterestsUser 表,但不会返回 Users 表的任何值。它只返回字段名称。

有什么问题吗?

更新:好的,上面生成的 SQL 是我从 Cake 的数据源 sql 日志中得到的:

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

为什么用户表值仅对所有字段返回 NULL?

更新:

好的,我在下面尝试过,但这工作正常......我在这里错过了什么!!??

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

最佳答案

连接条件的数组语法应该如下所示

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

相对于 array('User.id' => 'InterestsUser.user_id')。有关更多信息,请参阅 http://book.cakephp.org/view/1047/Joining-tables .

关于php - Cakephp 通过查找选项连接表不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7937642/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com