2013-03-02 85 views
0

我使用Cakephp2.3cakePHP2.3保存所有

我有以下各表

表1:用户
这里我有场GROUP_ID等领域 表2: 组 表3:学生

在我所字段USER_ID

表4:监护人

在我所提交user_id说明

而且student_guardians

其中i有场guardian_id,student_id数据,和relationship_id。

,而在其他表我们已经等领域名字,姓氏等。

我用蛋糕烘烤制作协会和

我想从学生进入所有数据/添加页面。像监护人(学生可以能够进入单的形式与他的详细资料提交多个gurdians)

我创建的视图如下

<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> 

,并在控制器功能补充的是

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.')); 
      } 
     } 

} 

,它不保存任何数据

以上所有代码已写入student_guradians控制器中。

是否有任何身体有想法如何实现这种数据 变种转储

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" } } } } 

感谢

+0

究竟哪个错误即将到来您可以放下吗?还是通过调试级别2进行检查?还有一个原因可能是你在模型关联中提到的关系..使用少量数据检查而不是一目了然检查一个... – Jhanvi 2013-03-02 05:45:00

+1

尝试'$ this-> GuardianStudent-> saveAll($ this-> request->数据)'这将为你工作。另外,如果你能向我们展示var_dump($ this-> request-> data)'。 – Rikesh 2013-03-02 05:52:38

+0

我正在检查少量数据。没有这样的错误。它只是显示无法保存。调试级别已经是2 – Rashmi 2013-03-02 05:53:13

回答

0

我意识到这是不是一个答案,但太多的信息把它放在'评论'

海事组织你有你的设计/逻辑缺陷。您设计了数据库,以便学生可以有多个监护人,并且监护人可以成为多个学生的监护人(HABTM)。这似乎是正确的,听起来合乎逻辑。

但是,你设计的形式,使得监护人的细节可以在同一时间,一个学生是添加,这(如果所有工作正常)将导致新的监护人将被插入到进入数据库在任何时候,即使这些监护人已经在数据库中。因此,基本上你已经在学生和监护人之间创建了一个'hasMany'关系,虽然'存储'为'HABTM'。

通过在表单中​​输入的监护人的细节,一个监护人将被插入,可能含有相同的资料(姓名,姓氏)作为现有卫,但具有不同的id,导致重复记录

我认为这会让你有几个选择;

  1. 添加新学生时,显示包含现有监护人的复选框(或多选)列表以供选择。根据数据库中存在的监护人数量,您可能需要创建一些“自定义”表单元素,以便于选择正确的Guardian,否则此列表可能会变得非常长
  2. 如果您只会先存储和监护人的姓氏,监护人不必由多个学生共享(即更改监护人的名字将使所有学生'附属于'该监护人)的名字改变,您可能会更好通过转换与hasMany/belongsTo关系的关系;例如一名监护人只附属于一名学生。

在任何情况下,问问自己是否只有守护者的名字/姓氏才足以唯一标识出正确的守护者。毕竟,守护者可能会使用同一个名字,并且您不想最终改变多名学生的守护者细节,事实上,他们不是同一个人,只有同一个名字?