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 6857497
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:01:39+00:00 2026-05-27T02:01:39+00:00

So I’m not sure how to do this in a cakey way. I’ve hacked

  • 0

So I’m not sure how to do this in a cakey way. I’ve hacked the crap out if it and now have something longer than straight php would be. I can’t find this anywhere in the docs. So here is how my tables are set up:

instruments HABTM teachers
teachers HABTM instruments
instruments_teachers is my Join table

now in the instruments_teachers table I have an extra field “type” which refers to whether it’s the teachers primary or secondary instrument.

I then have another table:

instrument_levels belongsTo instruments_teachers
instruments_teachers hasMany instrument_levels

So in summary:
A teacher can teach many instruments and instruments can belong to (or be taught) by many different teachers. Then the combination of teacher instrument is either the teachers primary or secondary instrument, and finally that combination can be taught at three different levels (defined by instrument_levels) beginning, intermediate, or advanced.

Let me know if you think there’s a better way to design the database, but it seems to make sense to me.

The problem comes when trying to use this data with cake. I can’t seem to figure it out. In my application the teachers will first select an instrument in a series of checkboxes, but the data must submit with the extra field type in the join table. Right now this is how I have the checkboxes appearing:

Controller:

$instruments = $this->Teacher->Instrument->find('list', array('fields' => array('id', 'instrument')));
$this->set(compact('instruments'));

View:

echo $this->Form->input('Instrument',array(
'label' => __('Instruments',true),
'type' => 'select',
'multiple' => 'checkbox',
'options' => $instruments,
'selected' => $this->Html->value('Instrument.Instrument'),
));

That displays all the check boxes together, but I would like more control and display each on separately (ie. a foreach loop). This doesn’t work though:

$checked = $this->Form->value('Instrument.Instrument');
echo $this->Form->label(__('Instruments', true));
foreach ($instruments as $id => $label) {
echo $this->Form->checkbox("Instrument", array(
    'checked' => (isset($checked[$id]) ? 'checked' : false),
    'hiddenField' => false,
    'value' => $id
    ));
    echo $this->Form->label("Instrument", ucfirst($label));
}

And how would I submit the extra field (“type” field) with the HABTM association? Finally I need to have checkboxes for what level the teachers can teach at. So I first need to fetch the HABTM data and then have 3 checkboxes next to the instrument. Seems easy enough, but how do I populate those check boxes after they’re added? When I fetch the data it just goes into a keyed array and doesn’t correlate to the level checkboxes. I have it in a foreach loop keyed in the form helper like so: InstrumentLevel.0.level, InstrumentLevel.1.level etc. But when the data is fetched it won’t correlate with the checkboxes correctly.

I know this was a lot, I have been working on this for a couple weeks, and I just don’t what I’m doing. If you could just lead me in the right direction I would appreciate it!

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-05-27T02:01:40+00:00Added an answer on May 27, 2026 at 2:01 am

    You cannot have an extra field in the join table. Each time you save your data the associations are lost, I quote

    The trouble is, hasAndBelongsToMany will not support this type of
    scenario because when hasAndBelongsToMany associations are saved, the
    association is deleted first. You would lose the extra data in the
    columns as it is not replaced in the new insert.

    For that you must use hasMany Through association.

    hasMany through (The Join Model)

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have a jquery bug and I've been looking for hours now, I can't
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
This could be a duplicate question, but I have no idea what search terms
I know there's a lot of other questions out there that deal with this
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have just tried to save a simple *.rtf file with some websites and

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.