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

  • Home
  • SEARCH
  • 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 8127593
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T07:35:09+00:00 2026-06-06T07:35:09+00:00

My question is in relation this this answer. https://stackoverflow.com/a/8773953/1297775 I have read at many

  • 0

My question is in relation this this answer.
https://stackoverflow.com/a/8773953/1297775

I have read at many places, what @deceze put as:
“To be quite honest, for any halfway complex application, relying on Cake’s automagic handling of HABTM relationships can get quite fragile and hard to debug, so I always manage HABTM records myself.”

Even the CakePHP book hints at it http://book.cakephp.org/2.0/en/models/saving-your-data.html#what-to-do-when-habtm-becomes-complicated

I want to ask, that when he says he ‘manages’ this HABTM records himself, does he…

1) Not create the HABTM relations in the models at all and create a model for the join table

OR

2) Create the HABTM relations in the models, but does not use it in the controller code by just using $this->FirstModel->JoinModel->saveAll($someData);

Thanks

  • 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-06-06T07:35:10+00:00Added an answer on June 6, 2026 at 7:35 am

    Basically you need use a hasManyThrough relationship, which is essentially a HABTM setup manually which allows you to add extra data to the relationship (such as created/expiry dates). It’s quite simple, you just need to create a model to join them and use the normal belongsTo and hasMany properties in the model, here is a simple user/membership/course setup:

    class User extends AppModel {
        public $hasMany = array('Membership');
    }
    
    class Course extends AppModel {
        public $hasMany = array('Membership');
    }
    
    class Membership extends AppModel {
        public $belongsTo = array('User', 'Course');
    }
    

    The only fields (at minimum) the memberships table needs is course_id and user_id.

    You can now operate on the Membership model as a normal model, without Cake treating it as HABTM and using its auto-magic whenever you save records. You can also use cool counterCaches on the membership model to count how many users a course has etc.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two models in relation one-to-many: class Question(db.Model): questionText = db.StringProperty(multiline=False) class Answer(db.Model):
In relation to this question ( Efficient hashCode() implementation ) I have one more
In relation to this stackoverflow question , how would I go about creating my
I have a question about Criteria method, one-to-many relation to the database, 'one' is
I have tried to answer this myself, by looking up several questions at StackOverflow.
This is in relation to this question . The proposed answers involve adding a
I have a double polymorphic associations, basically i'm going to put in relation Questions
Not sure what the correct title for this question should be. I have the
This might be a trivial question, but I have not found anything about it,
I read through the related questions and didn't find my answer. This isn't about

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.