Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 625765
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T19:20:43+00:00 2026-05-13T19:20:43+00:00

I am wrestling around a bit with CakePHP HABTM relationships, and I’ve gotten stuck

  • 0

I am wrestling around a bit with CakePHP HABTM relationships, and I’ve gotten stuck trying to do a seemingly simple task. I have many Camps and many NewsItems. Each NewsItem could be relevant to any of the camps, so when a user creates a NewsItem, they check which Camps the item is for. That’s the idea.

So…

I would like to incorporate some camp filtering into my NewsItems views. In other words, I would like to see all NewsItems relevant to Camp "A" and Camp "B".

Adding conditions to the "find" and "paginate" functions work like a charm.

However…

If a NewsItem belongs to multiple Camps, it appears multiple times in the list. So when I tell find to give me all NewsItems that belong to Camp "A" and Camp "B", a NewsItem that belongs to Camp "A" and Camp "B" will appear twice.

The debug query looks like this:

SELECT NewsItem.id, NewsItem.user_id, NewsItem.heading, NewsItem.body, NewsItem.modified, User.first_name FROM news_items AS NewsItem LEFT JOIN users AS User ON (NewsItem.user_id = User.id) LEFT JOIN camps_news_items AS CampsNewsItem ON (CampsNewsItem.news_item_id = NewsItem.id) WHERE CampsNewsItem.camp_id IN (1, 5) ORDER BY NewsItem.modified desc LIMIT 10

SELECT Camp.id, Camp.name, Camp.created, Camp.modified, CampsNewsItem.camp_id, CampsNewsItem.news_item_id FROM camps AS Camp JOIN camps_news_items AS CampsNewsItem ON (CampsNewsItem.news_item_id IN (6, 6, 7, 8) AND CampsNewsItem.camp_id = Camp.id) WHERE 1 = 1

The php code looks like this:

$camp_ids = array(1, 3, 6, 10);
$conditions = array('CampsNewsItem.camp_id' => $camp_ids);
    
$params['conditions'] = $conditions;
$this->NewsItem->bindModel(array('hasOne' => array('CampsNewsItem')));
$news_items = $this->NewsItem->find('all', $params);

Thanks for any insight!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T19:20:43+00:00Added an answer on May 13, 2026 at 7:20 pm

    Try this:

    $params['group'] = array('NewsItem.id');
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been wrestling with this seemingly simple re-direct task for hours now and for
I am wrestling with trying to implement a simple object-oriented like system in Tcl
We have been wrestling with the confines of ASP.NET trying to get JSONP working.
I have been wrestling with what should be a very simple thing for weeks
I have been wrestling with SvcUtil all day, trying to get it to generate
I've been wrestling around with this for awhile now. I am trying to make
I've been wrestling with Capistrano, trying to deploy a test rails app for a
I have been wrestling with this for a few days and after tons of
I'm wrestling with something that should be very simple - specify a sidebar at
A novice at WPF / XAML, I've been wrestling with layout and trying to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.