#5239: Incorrect HABTM primaryKey when accessing HABTM model "automagically"
----------------------------------+-----------------------------------------
Reporter: NOSLOW | Owner:
Type: Bug | Status: reopened
Priority: High | Milestone: 1.2.x.x
Component: Core Libs | Version: RC2
Severity: Major | Resolution:
Keywords: HABTM, primaryKey | Php_version: n/a
Cake_version: |
----------------------------------+-----------------------------------------
Changes (by nebbian):
* status: closed => reopened
* resolution: fixed =>
Comment:
The above patch works fine as long as you just use "id" as your primary
key on the join table.
In my case I was using a different schema and both versions (with and
without the patch) broke.
To get around it I added the following snippet on lines 718-726 of
Model.php (version 1.2.0.7962, released 25th Dec 2008, directly replaces
the line that was removed in the patch above, I have included some code
around the snippet so you can see where it lives)
{{{
'ds' => $this->useDbConfig
));
// ************ NEW CODE STARTS HERE ****************
// Patch BH 2009-01-14 because we often don't use id as our primary key
foreach($this->{$joinClass}->_schema as $joinClassFieldKey =>
$joinClassFieldValue){
if(isset($joinClassFieldValue["key"]) &&
($joinClassFieldValue["key"] == "primary")){
$this->{$joinClass}->primaryKey = $joinClassFieldKey;
}
}
// ************** END NEW CODE **************
} else {
$this->__constructLinkedModel($joinClass, $plugin .
$joinClass);
}}}
I tried to create a patch and test case for this to make you hardworking
CakePHP people's lives easier, but couldn't figure it out. My problems
involved:
{{{
class TipsMember extends AppModel {
var $name = 'TipsMember';
var $useTable = 'TipsMember';
var $primaryKey = 'TipsMemberID';
var $hasAndBelongsToMany = array(
'TipsCompetition' =>
array(
'className' => 'TipsCompetition',
'joinTable' => 'TipsMemberCompetition',
'foreignKey' => 'TipsMemberID',
'associationForeignKey' => 'TipsCompetitionID'
)
);
}
class TipsCompetition extends AppModel {
var $name = 'TipsCompetition';
var $useTable = 'TipsCompetition';
var $primaryKey = 'TipsCompetitionID';
}
class TipsMemberCompetition extends AppModel {
/* This is the join table, and has the following fields:
TipsMemberCompetitionID -> Primary
TipsMemberID
TipsCompetitionID
TipsMemberCompetitionJoinedDate -> This field was causing all the
problems.
*/
var $name = 'TipsMemberCompetition';
var $useTable = 'TipsMemberCompetition';
var $primaryKey = 'TipsMemberCompetitionID';
var $belongsTo = array(
'TipsMemberID' => array(
'className' =>
'TipsMember',
'foreignKey' =>
'TipsMemberID',
'conditions' => '',
'fields' => '',
'order' => ''
),
'TipsCompetitionID' => array(
'className' =>
'TipsCompetition',
'foreignKey' =>
'TipsCompetitionID',
'conditions' => '',
'fields' => '',
'order' => ''
),
);
}
}}}
I hope this helps someone.
--
Ticket URL: <https://trac.cakephp.org/ticket/5239#comment:3>
CakePHP : The Rapid Development Framework for PHP <https://trac.cakephp.org/>
Cake is a rapid development framework for PHP which uses commonly known design
patterns like ActiveRecord, Association Data Mapping, Front Controller and MVC.
Our primary goal is to provide a structured framework that enables PHP users at
all levels to rapidly develop robust web applications, without any loss to
flexibility.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"tickets cakephp" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/tickets-cakephp?hl=en
-~----------~----~----~----~------~----~------~--~---