【问题标题】:cakePHP2.3 Save AllcakePHP 2.3 保存
【发布时间】:2013-03-02 05:34:09
【问题描述】:

我正在使用 Cakephp2.3

我有以下表格

表 1:用户
这里我有字段 group_id 和其他字段 表 2: 团体 表 3:学生

我有字段 user_id

表 4:监护人

我在其中提交了 user_id

还有学生监护人

我有字段guardian_id、student_id 和relationship_id。

在其他表中,我们还有其他字段 firstname、lastname 等。

我使用蛋糕烘焙来创建关联和

我想输入来自学生/添加页面的所有数据。像监护人(学生可以在一个表单上输入多个监护人并提交他的详细信息)

我创建了如下视图

<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控制器中..

有没有人知道如何做到这一点 var转储数据

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

谢谢

【问题讨论】:

  • 到底是哪个错误,你能说一下吗?或者您是否通过调试级别 2 进行了检查?还有一个原因可能是您在模型关联中提到的关系..检查少量数据而不是一目了然...
  • 试试$this-&gt;GuardianStudent-&gt;saveAll($this-&gt;request-&gt;data) 这对你有用。另外,如果你能告诉我们var_dump($this-&gt;request-&gt;data)
  • 我正在检查少量数据。没有这样的错误。它只是显示无法保存。调试级别已经是 2
  • @Rikesh:使用 var 数据转储编辑的问题

标签: php cakephp cakephp-2.0 cakephp-model


【解决方案1】:

注意

我知道这不是一个答案,而是太多的信息不能放在“评论”中

IMO,您的设计/逻辑存在缺陷。您已经设计了数据库,以便一个学生可以拥有多个监护人,并且一个监护人可以是多个学生的监护人 (HABTM)。这似乎是正确的,听起来合乎逻辑。

然而,您设计了表单,以便在添加学生的同时输入监护人的详细信息,这(如果一切正常)将导致新的监护人一直插入到数据库中,即使这些监护人已经存在于数据库中。因此,基本上你已经在学生和它的监护人之间创建了一个“hasMany”关系,尽管“存储”为一个“HABTM”。

通过在表单中​​输入监护人的详细信息,将插入一个新的监护人,可能包含与现有监护人相同的详细信息(姓名、姓氏),但使用不同的id ,导致重复记录

我认为这会给您留下一些选择;

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

在所有情况下,问问自己是否只有监护人的名字/姓氏足以唯一识别正确的监护人。毕竟,监护人可能有相同的名字,而您不希望最终更改多个学生监护人的详细信息,而实际上他们不是同一个人,只是名字相同吗?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-12
    • 2013-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多