cakephp array results [maximum depth reached]

匿名 (未验证) 提交于 2019-12-03 02:29:01

问题:

i have two models. Teacher and Subject joined by HABTM defined both ways. A teacher can teach many subjects and a subject can be taught by many teachers.my join table is subjects_teachers and have fields id,teacher_id and subject_id.

Fetching Teacher data from its model, i expect all teachers and their respective subjects , Fetching Subject data from its model i expect to also see the teachers teaching that particular subject

problem on both instances, the associated model returns the correct number of records but the data is absent. i see [maximum depth reached] when i display the respective arrays.

I removed the id field from the join table and that fixed only the Teacher model.The Subject model still has the problem.

i just need to know what [maximum depth reached] means and why removing the id filed from the join table fixed the Teacher problem but not Subject.

also if its important i should mention that my Teacher model primary key field doesnt follow convention SUBJECT model

public $hasAndBelongsToMany = array(     'Teacher' => array(     'className' => 'Teacher',     'joinTable' => 'subjects_teachers',     'foreignKey' => 'subject_id',     'associationForeignKey' => 'teacher_id',     'unique' => 'keepExisting'     ) ); 

Teacher Model

public $hasAndBelongsToMany = array(     'Subject' => array(     'className' => 'Subject',     'joinTable' => 'subjects_teachers',     'foreignKey' => 'teacher_id',     'associationForeignKey' => 'subject_id',     'unique' => 'keepExisting'     ) ); 

Results from subject

array( (int) 0 => array(     'Subject' => array(         'id' => '1',         'subject_code' => '121',         'subject_name' => 'Mathematics',         'compulsory' => true     ),     'Teacher' => array(         (int) 0 => array(             [maximum depth reached]         ),         (int) 1 => array(             [maximum depth reached]         ),         (int) 2 => array(             [maximum depth reached]         )     ) ), 

Results from Teacher before removin id field

array( 'Teacher' => array(     'teacher_id' => '6',     'first_name' => 'George',    ),   'Subject' => array(     (int) 0 => array(         'id' => '1',         'subject_code' => '121',         'subject_name' => 'Mathematics',         'compulsory' => true,         'SubjectsTeacher' => array(             [maximum depth reached]         )     ) 

Results after removing id field

'Subject' => array(     (int) 0 => array(         'id' => '1',         'subject_code' => '121',         'subject_name' => 'Mathematics',         'compulsory' => true     ), 

回答1:

Your data exists, the debugger just wont display it because the depth option limits it. Use debug() (default depth = 25) or Debugger::dump()/exportVar() with a depth (second argument, defaults to 3) high enough for your deeply nested data.

See also



回答2:

It is related to model attribute recursive

http://book.cakephp.org/2.0/en/models/model-attributes.html#recursive



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!