I'm using Cake 1.2.6 and last night I noticed that a HABTM relationship wasn't being saved when I submit a form.
I have a HABTM relationship between Committee
and Volunteer
. The primary key for a Volunteer
is a UUID while the primary key for a Committee
is a human readable string (e.g. BOARDOFDIRECTORS
, FAIRCOMMITTEE
, FAIRASSOCIATES
, etc.). I have a form to create/edit volunteers and that form includes a select box whose options are exactly what you'd expect and are populated with options returned from Cake's find( 'list' )
method. Although I can't think of a reason it would matter, only one committee can be selected for a volunteer (the HABTM is for expected future needs).
Initial results show that selecting the BOARDOFDIRECTORS
option works as expected, but the others do not. Tracing the execution through the core code leads me to Model->__saveMulti()
where, in Line 1393, this code is executed:
$data[$this->hasAndBelongsToMany[$assoc]['foreignKey']] = $id;
If I dump $data
before that code, the output is FAIRASSOCIATES. Immediately after, its value is 4AIRASSOCIATES. It seems safe to assume that's why the relationship isn't being saved, but I haven't figured out why the data is changing at this point in the execution.
Has anyone else seen this? Am I missing some critical piece? To the best of my knowledge, this was working fine in v1.2.1 (I upgraded a week ago or so).
UPDATE
The first bit of apparent weirdness I see is that, although my $row
is a string, the condition in Line 1366 evaluates to true
so I drop into that code block. If my data is a string, how can it have a member value?
UPDATE
I clearly have some thinking to do, but here's the bottom line. If I drop log writes immediately before and immediately after Line 1394 like so:
$this->log( 'Setting ' . $data . '[' . $this->hasAndBelongsToMany[$assoc]['foreignKey'] . '] = ' . $id, LOG_DEBUG );
$data[$this->hasAndBelongsToMany[$assoc]['foreignKey']] = $id;
$this->log( 'Creating ' . json_encode( $data ) . ' on ' . $join, LOG_DEBUG );
The relevant output is:
2010-03-05 18:57:08 Debug: Setting FAIRASSOCIATES[volunteer_id] = 4b78717f-8ad4-4671-b81c-4e8745591fb4
2010-03-05 18:57:08 Debug: Creating "4AIRASSOCIATES" on CommitteesVolunteer
Possible issues:
- I'm not sure how/why Cake is trying to set the
volunteer_id
member on a string - "FAIRASSOCIATES" is the ID of a committee to which a volunteer is grouped, not a model of any sort, so I don't understand the relevance of
FAIRASSOCIATES[volunteer_id]
at all. - I have no idea how or why the value of
$data
is being morphed into4AIRASSOCIATES
by that one line of code.