Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Well, I managed to find a solution to this. I was waiting to see if anyone found something better, as I'm pretty sure my solution is far from the best, but here it goes, in case someone finds this question in the future.</p> <p>My issue was that I needed, for every group I selected to register assitance:</p> <ol> <li><strong>Every student</strong> that belonged to that group</li> <li><strong>All the assitances</strong> of the students belonging to that group</li> </ol> <p>Now, in order to make use of the <a href="http://book.cakephp.org/view/182/Form" rel="nofollow noreferrer">Form helper</a> to edit those assitences (and after, the <a href="http://book.cakephp.org/view/75/Saving-Your-Data" rel="nofollow noreferrer">saveAll()</a> method), I needed <code>$this-&gt;data</code> to have ann array like this:</p> <pre><code>Array ( [Assitance] =&gt; Array ( [5] =&gt; Array ( [id] =&gt; 5 [date] =&gt; 2011-01-09 [assitance] =&gt; A [updated] =&gt; 2011-01-16 21:32:00 [created] =&gt; 2011-01-16 21:32:00 ) [6] =&gt; Array ( [id] =&gt; 6 [date] =&gt; 2011-03-09 [assitance] =&gt; A [updated] =&gt; 2011-01-16 21:32:00 [created] =&gt; 2011-01-16 21:32:00 ) ) ) </code></pre> <p>at the same time, though, I needed <em>each student linked to his or her assitances</em>, to be able to loop trough them, and display the edit forms for every assitance in a table like the one in the question:</p> <p><img src="https://i.stack.imgur.com/1cKI1.jpg" alt="alt text"></p> <p>What I ended up doing is this</p> <ol> <li>First, I <strong>query all the students belonging to the selected group</strong>.</li> <li>For each student <strong>get all his/her assitances</strong> .</li> </ol> <p>so, for each student, I ended up having an array like this:</p> <pre><code>Array ( [Student] =&gt; Array ( [id] =&gt; 12 [last_name] =&gt; LASARTE [name] =&gt; Julia [created] =&gt; 2011-01-08 16:35:00 [updated] =&gt; 2011-01-08 16:35:00 [assitance] =&gt; Array ( [0] =&gt; Array ( [Assitance] =&gt; Array ( [id] =&gt; 4 [date] =&gt; 2011-01-09 [assitance] =&gt; z [updated] =&gt; 2011-01-16 20:51:00 [created] =&gt; 2011-01-16 20:51:00 ) [assitance_studenty] =&gt; Array ( [id] =&gt; 2 [student_id] =&gt; 12 [assitance_id] =&gt; 4 [comision] =&gt; 0 ) ) ) ) ) ) </code></pre> <p>Now, with that, I have the information I need in order to display the table and create the forms, but I still need the assitances data in <code>$this-&gt;data</code>, so the form helper can create the from displaying the correct information, and afterwars, saveAll() updates the rows in the database correctly.</p> <p>What I needed to do was turn the array of <code>$students</code> into an structure like the one at the beginning of the post. Enter the <a href="http://book.cakephp.org/view/640/Set" rel="nofollow noreferrer">Set Class</a>:</p> <pre><code>$formated_students = Set::combine($students, '{n}.students.id', '{n}.students'); $assitance = Set::extract('/presentes/Assistance', $formated_students); $this-&gt;data['Assistance'] = Set::combine($assistance, '{n}.Assistance.id', '{n}.Assistance'); </code></pre> <p>I'm pretty sure the first to lines (formating the students array in order to extract the assitance) can be done with just one <code>Set::extract</code> or <code>Set::classicExtract</code>, but this worked and regular expressions are really not my thing, so. </p> <p>After that, It's just a matter of looping trough the data and making the table:</p> <pre><code>&lt;?php echo $form-&gt;create('Assistance', array('url' =&gt; array('controller' =&gt; 'Comisions', 'action' =&gt; 'register_assitance'))); foreach ($students as $student) { ?&gt; &lt;tr&gt;&lt;td&gt;&gt;&lt;?php echo $student['students']['last_name'].", ".$student['students']['name']; ?&gt;&lt;/td&gt; &lt;?php foreach ($student['students']['assitance'] as $asstiance) { ?&gt; &lt;td&gt;&lt;?php echo $form-&gt;input('Assistance.'.$assitance['Assistance']['id'].'.id'); echo $form-&gt;input('Assistance.'.$assitance['Assistance']['id'].'.assistance', array( 'label' =&gt; false, 'size' =&gt; 1)) ?&gt;&lt;/td&gt; &lt;?php } ?&gt; &lt;/tr&gt; &lt;?php } ?&gt; </code></pre>
    singulars
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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