0

当注册操作尝试保存数据时,没有任何反应并且用户返回到同一页面

经过一些调试后,我在运行 '$this->Participant->create()' 时发现它没有返回新的 id

if ($this->request->is('post') || $this->request->is('put')) {
    if (!isset($this->data['Participant']['typeSel'])) {
        $regType = $this->data['Participant']['type'];

        // for pilots and visitors, copy the name/email from TeamMember to Participant
        if ($regType == 'pilot' || $regType == 'visitor') {
            $this->request->data['Participant']['name'] =
                    ucwords(strtolower($this->request->data['TeamMember'][0]['first_name'])) . ' '
                            . ucwords(strtolower($this->request->data['TeamMember'][0]['family_name']));
            $this->request->data['Participant']['email'] = $this->request->data['TeamMember'][0]['email'];
        }

        // mark valid inside the team (if not joining)
        if ($regType != 'jointeam') {
            foreach ($this->request->data['TeamMember'] as & $teamMember) {
                $teamMember['validate_inteam'] = 'yes';
                if ($regType == 'visitor') {
                    $teamMember['free_entrance'] = 'yes';
                    $teamMember['num_tables'] = 0;
                }
            }
            $numTeamMembers = count($this->data['TeamMember']);
        }

        // set password
        $this->request->data['Participant']['password'] =
                Security::hash($this->data['Participant']['password1'], null, true);

        // perform registration itself
        if ($regType == 'jointeam') {
            $this->TeamMember->create($this->request->data);
            if ($this->TeamMember->save($this->request->data)) {
                $this->Session->setFlash('Your registration was recorded. An email was dispatched to the team coordinator for approval.');

                $this->Mail->sendTeamValidationEmail(
                        $this->Participant->findById($this->data['TeamMember']['participant_id']));
                $this->redirect(array(
                        'controller' => 'pages',
                        'action' => 'welcome'));
            }
            else {
                $this->Session->setFlash('Your registration could not be completed. Please try again.');
            }
        }
        else {
            $this->Participant->create(); // $this->Participant->id remains empty
            if ($this->Participant->saveAll($this->request->data)) {
                $this->Session->setFlash('Your registration was recorded. You will receive an email with further details during the next 24 hours.');

                $this->Mail->sendConfirmEmail($this->Participant->findById($this->Participant->id));
                $this->redirect(array(
                        'controller' => 'pages',
                        'action' => 'welcome'));
            }
            else {
                $this->Session->setFlash('Your registration could not be completed. Please try again.');
            }
        }
    }
}

如何获得创建函数来添加 id。DB似乎没有问题,因为您可以登录并在其他控制器上获取数据

4

1 回答 1

2

您误解了这些方法的作用。

$this->Participant->create();

它清除模型(取消设置 id),与您期望的完全相反。Cake 需要这个方法才能真正使用 save()、saveMany()、saveAll() 等创建新记录。

然后这些方法再次设置 Model->id (当然是在成功保存之后)。

此外,使用 saveAll() 如果您需要这些 ID,您应该查看回调,因为它只能为您提供最后存储的记录的 id,然后以这种方式(它将在每次保存时覆盖它)。

于 2013-10-13T11:29:14.420 回答