#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:  invalid =>

Comment:

 Hi TommyO,

 Thanks for pointing me in the direction of the changeset, although I
 couldn't find any notes on that page to explain anything.  Using 'with'
 may well get around the issue, however this still doesn't address the BUG
 that causes the following error:

 ''Notice (8): Undefined index:  id [CORE/cake/libs/model/model.php, line
 1293]''

 ''Notice (8): Undefined index:  id [CORE/cake/libs/model/model.php, line
 1294]''

 ''Warning (2): Cannot modify header information - headers already sent by
 (output started at /usr/lib/cake/cake/libs/debugger.php:504)
 [CORE/cake/libs/controller/controller.php, line 615]''


 The join table is being set up with the default primary key name ('id').
 If my join table has a different primary key name, as well as an extra
 field, then you get the above error.  I don't think that it is necessary
 to have a join table model, although I put it in the above error report
 for clarity.

 Here is a var_dump of the affected variable:


 '''Before my fix (note the wrong primary key)'''

 {{{
 object(AppModel)#28 (37) {
   ["useDbConfig"]=>
   string(7) "default"
   ["useTable"]=>
   string(21) "TipsMemberCompetition"
   ["displayField"]=>
   bool(false)
   ["id"]=>
   bool(false)
   ["data"]=>
   array(0) {
   }
   ["table"]=>
   string(21) "TipsMemberCompetition"
   ["primaryKey"]=>
   string(2) "id"                <=== ******* WRONG!!!!  **********
   ["_schema"]=>
   array(4) {
     ["TipsMemberCompetitionID"]=>
     array(5) {
       ["type"]=>
       string(7) "integer"
       ["null"]=>
       bool(false)
       ["default"]=>
       NULL
       ["length"]=>
       int(11)
       ["key"]=>
       string(7) "primary"     <=== ******* This is our primary key
 **********
     }
     ["TipsMemberID"]=>
     array(4) {
       ["type"]=>
       string(7) "integer"
       ["null"]=>
       bool(false)
       ["default"]=>
       string(1) "0"
       ["length"]=>
       int(11)
     }
     ["TipsCompetitionID"]=>
     array(4) {
       ["type"]=>
       string(7) "integer"
       ["null"]=>
       bool(false)
       ["default"]=>
       string(1) "0"
       ["length"]=>
       int(11)
     }
     ["TipsMemberCompetitionJoinedDate"]=>
     array(4) {
       ["type"]=>
       string(8) "datetime"
       ["null"]=>
       bool(false)
       ["default"]=>
       string(19) "0000-00-00 00:00:00"
       ["length"]=>
       NULL
     }
   }
   ["validate"]=>
   array(0) {
   }
   ["validationErrors"]=>
   array(0) {
   }

   (etc... snipped for clarity)
 }}}


 '''After my fix (note the correct primary key) '''
 {{{
 object(AppModel)#28 (37) {
   ["useDbConfig"]=>
   string(7) "default"
   ["useTable"]=>
   string(21) "TipsMemberCompetition"
   ["displayField"]=>
   bool(false)
   ["id"]=>
   bool(false)
   ["data"]=>
   array(0) {
   }
   ["table"]=>
   string(21) "TipsMemberCompetition"
   ["primaryKey"]=>
   string(23) "TipsMemberCompetitionID"   <=== ******* Ahhh that's better
 **********
   ["_schema"]=>
   array(4) {
     ["TipsMemberCompetitionID"]=>
     array(5) {
       ["type"]=>
       string(7) "integer"
       ["null"]=>
       bool(false)
       ["default"]=>
       NULL
       ["length"]=>
       int(11)
       ["key"]=>
       string(7) "primary"          <=== ******* Makes sense really
 **********
     }
     ["TipsMemberID"]=>
     array(4) {
       ["type"]=>
       string(7) "integer"
       ["null"]=>
       bool(false)
       ["default"]=>
       string(1) "0"
       ["length"]=>
       int(11)
     }
     ["TipsCompetitionID"]=>
     array(4) {
       ["type"]=>
       string(7) "integer"
       ["null"]=>
       bool(false)
       ["default"]=>
       string(1) "0"
       ["length"]=>
       int(11)
     }
     ["TipsMemberCompetitionJoinedDate"]=>
     array(4) {
       ["type"]=>
       string(8) "datetime"
       ["null"]=>
       bool(false)
       ["default"]=>
       string(19) "0000-00-00 00:00:00"
       ["length"]=>
       NULL
     }
   }
   ["validate"]=>
   array(0) {
   }
   ["validationErrors"]=>
   array(0) {
   }

 }}}

 My fix goes through the fields, and if it finds one that is marked
 "Primary", then it sets the models primary key to that field.  Really
 simple, and it works.
 [[BR]]


 If anyone else is having the same trouble, then change your code to use
 "with".[[BR]][[BR]]
 I really hope that I'm the only person who lost a days work chasing down
 this bug, searching trac, figuring out the problem, fixing the problem,
 writing up a detailed error report, only to have it thrown in my face.
 [[BR]][[BR]]
 I must say I am disappointed at the response that I have received from the
 cake community.  I'm going to think twice before posting anything again.
 [[BR]]

-- 
Ticket URL: <https://trac.cakephp.org/ticket/5239#comment:7>
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to