CakePHP 2.4:Model-> create()不返回id

时间:2013-10-13 11:19:37

标签: php cakephp auto-increment

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

经过一些调试后,我发现在运行'$ 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.');
            }
        }
    }
}

如何让create函数添加id。 数据库似乎没有问题,因为您可以登录并获取其他控制器上的数据

1 个答案:

答案 0 :(得分:2)

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

$this->Participant->create();

它清除模型(取消设置id),与你期望的完全相反。 Cake实际上可以使用save(),saveMany(),saveAll()等创建新记录。

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

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