I am using Cakephp2.3
I have following tables
Table 1 : Users
Here i have field group_id and other fields Table 2 : Groups Table 3 : Students
in which i have field user_id
Table 4 : Guardians
in which i have filed user_id
And student_guardians
where i have field guardian_id, student_id, and relationship_id.
and in others tables we have other fields firstname, lastname etc..
i used cake bake for creating associations and
i want to enter all data from students/add page. like guardians(student can able to enter multiple gurdians on single form submit with his details)
I created a view as following
<div class="guardianStudents form">
<?php echo $this->Form->create('GuardianStudent'); ?>
<fieldset>
<legend><?php echo __('Add Guardian Student'); ?></legend>
<?php
echo $this->Form->input('Student.first_name');
echo $this->Form->input('Guardian.0.Guardian.first_name');
echo $this->Form->input('Guardian.0.Guardian.last_name');
echo $this->Form->input('Guardian.0.User.username');
echo $this->Form->input('Guardian.0.User.password');
echo $this->Form->input('Guardian.0.User.group_id',array('type'=>'text','value'=>'1'));
echo $this->Form->input('Guardian.1.Guardian.last_name');
echo $this->Form->input('Guardian.1.Guardian.last_name');
echo $this->Form->input('Guardian.1.User.username');
echo $this->Form->input('Guardian.1.User.password');
echo $this->Form->input('Guardian.1.User.group_id',array('type'=>'text','value'=>'1'));
echo $this->Form->input('Student.User.group_id',array('type'=>'text','value'=>'1'));
echo $this->Form->input('Student.User.username');
echo $this->Form->input('Student.User.password');
?>
</fieldset>
<?php echo $this->Form->end(__('Submit')); ?>
</div>
<div class="actions">
<h3><?php echo __('Actions'); ?></h3>
<ul>
<li><?php echo $this->Html->link(__('List Guardian Students'), array('action' => 'index')); ?></li>
<li><?php echo $this->Html->link(__('List Guardian Relationships'), array('controller' => 'guardian_relationships', 'action' => 'index')); ?> </li>
<li><?php echo $this->Html->link(__('New Guardian Relationship'), array('controller' => 'guardian_relationships', 'action' => 'add')); ?> </li>
<li><?php echo $this->Html->link(__('List Students'), array('controller' => 'students', 'action' => 'index')); ?> </li>
<li><?php echo $this->Html->link(__('New Students'), array('controller' => 'students', 'action' => 'add')); ?> </li>
<li><?php echo $this->Html->link(__('List Guardians'), array('controller' => 'guardians', 'action' => 'index')); ?> </li>
<li><?php echo $this->Html->link(__('New Guardian'), array('controller' => 'guardians', 'action' => 'add')); ?> </li>
</ul>
</div>
and in controller function add is
public function add() {
if ($this->request->is('post')) {
$this->GuardianStudent->create();
$this->loadModel('User');
if ($this->GuardianStudent->saveAll($this->request->data['Guardian'])) {
$this->Session->setFlash(__('The guardian student has been saved'));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The guardian student could not be saved. Please, try again.'));
}
}
}
and it doesnot saving any data
All Above code has been writen in student_guradians controller ..
Does Any body have idea how to achieve this var dump of data
array(2) { ["Student"]=> array(2) { ["first_name"]=> string(5) "jkjkj" ["User"]=> array(3) { ["group_id"]=> string(1) "1" ["username"]=> string(10) "klklfkfkkl" ["password"]=> string(6) "lklklk" } } ["Guardian"]=> array(2) { [0]=> array(2) { ["Guardian"]=> array(2) { ["first_name"]=> string(5) "jkjkj" ["last_name"]=> string(8) "kjkjkjkj" } ["User"]=> array(4) { ["username"]=> string(7) "kjkjkjk" ["password"]=> string(7) "kjkjjkk" ["group_id"]=> string(1) "1" ["name"]=> string(1) "1" } } [1]=> array(2) { ["Guardian"]=> array(1) { ["last_name"]=> string(6) "jkkjkj" } ["User"]=> array(4) { ["username"]=> string(10) "jkljjljjkl" ["password"]=> string(6) "kjkjkj" ["group_id"]=> string(1) "1" ["name"]=> string(1) "1" } } } }
Thanks
note
I realise this is not an answer, but too much information to put it in a 'comment'
IMO you have a flaw in your design/logic. You've designed your database so that a Student can have multiple Guardians and a Guardian can be a Guardian for multiple Students (HABTM). This seems to be right and sounds logical.
However, you designed the form so that the details of Guardians can be entered at the same time that a Student is added, which (if all works ok) will cause new Guardians to be inserted into the database at all times, even if those Guardians were already present in the database. Because of this, basically you've created a 'hasMany' relationship between the Student and its Guardians, although 'stored' as a 'HABTM'.
By entering the details for the Guardians in the form, a new Guardian will be inserted, possibly containing the same details (name, last_name) as an existing Guardian, but with a different id
, causing duplicate records
I think this will leave you with a few options;
In all cases, ask yourself if only First name/ Last name of a Guardian is sufficient to uniquely identify the right Guardian. After all, Guardians may share the same name, and you don't want to end up changing details of the Guardian of multiple Students when, in fact, they were not the same person, only having the same name?