Note that there are some explanatory texts on larger screens.

plurals
  1. POCakePHP Containable showing wrong data
    primarykey
    data
    text
    <p>I have been looking around for a long time regarding the CakePHP containable, and somehow I get the impression there is a mistake in the CakePHP code... Let me give you the following example</p> <p>Assume a Model "Accounting" that is related to "Instructions". </p> <pre><code>public $belongsTo = array('Instruction'); </code></pre> <p>I do a join like this:</p> <pre><code>$options['contain'] = array( 'Instruction' =&gt; array( 'fields' =&gt; array('Instruction.id', 'Instruction.main_student_id', 'Instruction.teacher_id'), 'Student' =&gt; array( 'fields' =&gt; array('Student.id', 'Student.full_name', 'Student.invoice_payer_id'), ), 'Teacher' =&gt; array( 'fields' =&gt; array('Teacher.id', 'Teacher.full_name'), 'conditions' =&gt; array('Teacher.id' =&gt; $teacher_id) ), ) ); return parent::find('all', $options); </code></pre> <p>So there is an instruction and to this instruction a student and a teacher belong to. I call find from the "Accounting" model</p> <h2>What I expect</h2> <ol> <li>the joins are done automatically and correct</li> <li>the fields mentioned in the contain are retrieved</li> </ol> <h2>What I get</h2> <ol> <li>the fields are joined, but wrongly; so the result array contains an Instruction with a Student and a Teacher. But instead of displaying the correct Teacher belonging to an Instruction, it always displays the one specified in the condition. </li> </ol> <p>Example:</p> <ul> <li>Assume the table Instruction related to the Teachers "1 - John Appleseed","2 - Peter Googleseed", "3 Larry Microseed"</li> <li>Assume multiple Instructions belonging to the 3 Teachers</li> <li>if $teacher_id = 3 the query returns ALL Instruction, and instead of displaying the correct Teacher belonging to the Instruction, always the Teacher with the id = 3 is displayed; to be more precise: [Instruction][teacher_id] is set to the correct value, but the [Instruction][Teacher] is always set to the teacher of the condition</li> </ul> <p>But let's go a step further:</p> <ol> <li>if $recursive = -1 is set then I only get returned only the fields of the model "Accounting"; the fields of Instruction, Student, Teacher are not retrieved</li> </ol> <h2>Let's make sure</h2> <ul> <li>I put "public $actsAs = array('Containable');" in AppModel from which all the other Models inherit</li> <li>Yes, Instruction has a "$belongsTo = array('Teacher', 'Student' => array(...))"</li> <li>The fields necessary for joining are all chosen</li> </ul> <h2>So, what?</h2> <p>Could you please try to help me to understand... - why the joins are done wrongly? - why not all the fields are retrieved?</p> <h2>I tried already</h2> <ul> <li>To set the joins explicitly: very weird, then [Instruction][teacher_id] is empty and [Instruction][Teacher] is not set!</li> <li>To leave out the explicit choice of the fields being retrieved</li> </ul>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload