cakePHP와 관련된 문제가 있습니다.CakePHP - 연관성있는 조건 찾기
클럽, hasMany의 포스트
클럽 HABTM 사용자 기본적으로 내 clubs_users 테이블은 또한의이 '제한'과 '을 가정 해 봅시다 같은 추가 필드가 포함
: 나는 클럽 모델, 사용자 모델과 게시물 모델을 가지고 diff '는 사용자가 표시하려는 최대 게시물 수와 해당 게시물의 허용 기간을 각각 나타냅니다. 특정 사용자와 관련된 모든 클럽에 적합한 게시물을 선택하고 싶습니다. 이 같은 someting을하고 있어요
$clubs = $this->Club->User->find('first', array(
'conditions' => array('User.id' => $id),
'contain' => array(
'Club' => array(
'fields' => array(
'id',
'name'
),
'Post' => array(
'fields' => array(
'id',
'title',
'description',
'created',
),
'order' => array('Post.created' => 'DESC'),
'conditions' => array(
'DATEDIFF(NOW(), `Post.created`) <= /* 1 */',
),
'limit' => '/* 2 */'
)
)
)
));
이 작업을하려면 1과 2 대신 무엇을 넣어야합니까? ClubsUser.diff와 ClubsUser.limit를 시도했지만 where 절에서 해당 필드를 알 수 없다는 오류가 발생했습니다.
도움이 될 것입니다.
읽어 주셔서 감사합니다.
편집
는 bancer의 코멘트 후에, 나는 MySQL의 문서에 깊은 모습과는 LIMIT는 숫자 인수를 기대 나타났다. 그래서 나는 이제 너무 오래되지 않은 게시물을 반환하기를 원합니다.
이SELECT * FROM `users` AS `Follower`
WHERE `Follower`.`id` = 1
LIMIT 1
SELECT * FROM `clubs` AS `Club`
JOIN `clubs_users` AS `ClubsUser`
ON (`ClubsUser`.`user_id` = 1 AND `ClubsUser`.`club_id` = `Club`.`id`)
ORDER BY `ClubsUser`.`role_id` DESC
SELECT * FROM `posts` AS `Post`
WHERE DATEDIFF(NOW(), `Post`.`created`) <= `ClubsUser`.`max_time_posts` AND `Post`.`club_id` = (1)
ORDER BY `Post`.`created` DESC
LIMIT 10
마지막 쿼리를 반환 나의 새로운 발견은
$overview = $this->Club->Follower->find('first', array(
'conditions' => array('Follower.id' => $this->Auth->user('id')),
'contain' => array(
'Club' => array(
'fields' => array(
'id',
'name'
),
'Post' => array(
'fields' => array(
'id',
'title',
'description',
'created',
),
'order' => array('Post.created' => 'DESC'),
'conditions' => array(
'DATEDIFF(NOW(), `Post.created`) <= ClubsUser.max_time_posts',
),
'limit' => 10
)
)
)
));
그것은 다음 세 가지 SQL 쿼리 (내가 명확성의 이유로 * 이름 필드를 대체)를 생성 (실제 필드 이름)입니다 오류 :
이상적 절은, 내가 하나에 가까운 쿼리 아래 대신 위의 마지막 두 쿼리를 좀하고 싶습니다 곳에서 필드 'ClubsUser.max_time_posts'알 수없는 :
SELECT * FROM `clubs` AS `Club`
JOIN `clubs_users` AS `ClubsUser`
ON (`ClubsUser`.`user_id` = 1 AND `ClubsUser`.`club_id` = `Club`.`id`)
LEFT JOIN `posts` AS `Post`
ON (`Post`.`club_id` = `Club`.`id` AND DATEDIFF(NOW(), `Post`.`created`) <= `ClubsUser`.`max_time_posts`)
ORDER BY `ClubsUser`.`role_id` DESC, `Post`.`created` DESC
LIMIT 10
아이디어가 있으십니까?
를 생산? 디버그 출력에서 복사합니다. 어떤 SQL 쿼리가 필요합니까? – bancer
@bancer : 내 질문을 편집했습니다 –