1
관심 분야 및 사용자 모델 ID가있는 테이블에서 내 테이블 hasMany를 통해 사용자 테이블에 참여하려고합니다.find 옵션을 통해 테이블을 조인하는 Cakephp가 작동하지 않습니다.
$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 테이블 벌금을 반환하지만 사용자 테이블의 모든 값을 반환하지 않습니다
다음은 옵션으로 찾기 쿼리입니다. 필드 이름 만 반환합니다.
무엇이 잘못 될 수 있습니까?
UPDATE : OK, 위의 나는 케이크의 데이터 소스의 SQL 로그에서 가져온 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 반환?
업데이트 : 아래 시도했지만이 제대로 작동
OK ... 내가 무슨 말이냐 !! ?? array('User.id' => 'InterestsUser.user_id')
반대로
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
확인 문제가 발견되었습니다. 이 InterestsUser.user_id 필드입니다! 분명히 나는이 일을하기 위해 꺼내야한다! 그러나 이것이 왜 일어나는 것입니까? Cake의 데이터 소스에 의해 추가 된 것처럼 이것을 어떻게 제거 할 수 있습니까? 난 정말 사용자 정의 SQL 쿼리를 사용하고 싶지 않아 ... –
'conditions => array ('User.id = InterestsUser.user_id')'시도 했습니까? –
초기 Cake 옵션 쿼리를 살펴보십시오. 조인 배열에서 이미 그 값을 가지고 있습니다! –