I'm developing a simple application with CakePhp, and need some help on creating a multi-record edit form using some related data.
The application I'm developing is pretty straightforward, its main purpose is managing students records: updating, deleting, changing a student from one group to another, the usual suspects.
The relevant tables of the database are:
group = (id, teacher, classroom, etc)
groups_students=(groupID, studentID, since, until) students = (id, name, last_name, etc) assitance = (id, assitance, date) assistance_students (studentID, assitanceID, meta_information )As you might have gathered from the tables above, the application is supposed to aid in recording assitance. Which is where I'm having some issues.
What I want to do is this:
- Have the user select a group
- In de detail group, I'll have an action called "Register Assistance"
Register assitance should redirect to a view in wich for every student belonging to that group, the user can see the student's assitance, edit them, and save. Something like this:
In which A stands for "Absent" and P for "Present" and the user can edit everyone, and the save.
I just don't know how to go about that? How do I manage that? I've managed to create a multi-edit form for the assitance, but adding the related data is a pain, I don't know if I should query the students from the groups controllers and then pass that to the action to register assitance, or manage all the logic inside the assitance controller?
Any help would be great,
thanks!
Edit: Here's the output of $this->Student->find('first');
Array (
[Alumno] => Array (
[id] => 14
[tipo] => dni
[dni] => 2321312312
[apellido] => COQUITO
[nombre] => Pepe
[carrera] => Composición Musical
开发者_运维百科 [creado] => 2011-01-08 17:59:00
[modificado] => 2011-01-08 17:59:00
)
)
The output is in spanish. Alumno = Student, nombre= first name, apellido= last_name.
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.
My issue was that I needed, for every group I selected to register assitance:
- Every student that belonged to that group
- All the assitances of the students belonging to that group
Now, in order to make use of the Form helper to edit those assitences (and after, the saveAll() method), I needed $this->data
to have ann array like this:
Array
(
[Assitance] => Array
(
[5] => Array
(
[id] => 5
[date] => 2011-01-09
[assitance] => A
[updated] => 2011-01-16 21:32:00
[created] => 2011-01-16 21:32:00
)
[6] => Array
(
[id] => 6
[date] => 2011-03-09
[assitance] => A
[updated] => 2011-01-16 21:32:00
[created] => 2011-01-16 21:32:00
)
)
)
at the same time, though, I needed each student linked to his or her assitances, to be able to loop trough them, and display the edit forms for every assitance in a table like the one in the question:
What I ended up doing is this
- First, I query all the students belonging to the selected group.
- For each student get all his/her assitances .
so, for each student, I ended up having an array like this:
Array
(
[Student] => Array
(
[id] => 12
[last_name] => LASARTE
[name] => Julia
[created] => 2011-01-08 16:35:00
[updated] => 2011-01-08 16:35:00
[assitance] => Array
(
[0] => Array
(
[Assitance] => Array
(
[id] => 4
[date] => 2011-01-09
[assitance] => z
[updated] => 2011-01-16 20:51:00
[created] => 2011-01-16 20:51:00
)
[assitance_studenty] => Array
(
[id] => 2
[student_id] => 12
[assitance_id] => 4
[comision] => 0
)
)
)
)
)
)
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 $this->data
, so the form helper can create the from displaying the correct information, and afterwars, saveAll() updates the rows in the database correctly.
What I needed to do was turn the array of $students
into an structure like the one at the beginning of the post. Enter the Set Class:
$formated_students = Set::combine($students, '{n}.students.id', '{n}.students');
$assitance = Set::extract('/presentes/Assistance', $formated_students);
$this->data['Assistance'] = Set::combine($assistance, '{n}.Assistance.id', '{n}.Assistance');
I'm pretty sure the first to lines (formating the students array in order to extract the assitance) can be done with just one Set::extract
or Set::classicExtract
, but this worked and regular expressions are really not my thing, so.
After that, It's just a matter of looping trough the data and making the table:
<?php
echo $form->create('Assistance', array('url' => array('controller' => 'Comisions', 'action' => 'register_assitance')));
foreach ($students as $student) { ?>
<tr><td>><?php echo $student['students']['last_name'].", ".$student['students']['name']; ?></td>
<?php foreach ($student['students']['assitance'] as $asstiance) { ?>
<td><?php echo $form->input('Assistance.'.$assitance['Assistance']['id'].'.id');
echo $form->input('Assistance.'.$assitance['Assistance']['id'].'.assistance', array( 'label' => false, 'size' => 1)) ?></td>
<?php } ?>
</tr>
<?php } ?>
精彩评论