0

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

Rashmi
  • 551
  • 2
  • 4
  • 17
  • exactly which error is coming can you please put that? OR have you checked by making debug level 2? also one reason can be relations you have mentioned in your model association..check with small number of data instead of checking in at glance one... – Jhanvi Mar 02 '13 at 05:45
  • 1
    Try `$this->GuardianStudent->saveAll($this->request->data)` this will work for for you. Also if you can show us `var_dump($this->request->data)`. – Rikesh Mar 02 '13 at 05:52
  • i am checking small number of data. there is no such error. it simply shows could not save . debug level is already 2 – Rashmi Mar 02 '13 at 05:53
  • @Rikesh: question edited with var dump of data – Rashmi Mar 02 '13 at 06:01

1 Answers1

0

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;

  1. When adding new students, present a list of checkboxes (or a Multi-select) containing existing Guardians to choose from. Depending on the number of Guardians present in your database, you may need to create some 'custom' form element to make it easier to select the right Guardian, otherwise this list might become very long
  2. If you'll be only storing first and last name of the Guardians, and Guardians don't have to be shared by multiple studens (i.e. changing the first name of a Guardian will make it change for all Students 'attached to' that Guardian), you may be better off by converting the relation to a hasMany/belongsTo relation; e.g. a Guardian is attached only to a single Student.

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?

thaJeztah
  • 27,738
  • 9
  • 73
  • 92