使用 cakephp 保存数据不起作用

发布于 12-15 11:47 字数 1642 浏览 3 评论 0原文

我正在尝试使用 CakePHP 2.0 加载编辑保存记录,但我得到了 save 方法期间出现一般错误,这无助于我理解问题出在哪里。

如果我尝试使用 debug($this->User->invalidFields()); 我得到一个 空数组,但我得到 false 来自 $this->User->save() 条件。

这是我收到错误的控制器操作:

public function activate ($code = false) {
    if (!empty ($code)) {

        // if I printr $user I get the right user
        $user = $this->User->find('first', array('activation_key' => $code));

        if (!empty($user)) {
            $this->User->set(array (
                'activation_key' => null,
                'active' => 1
            ));

            if ($this->User->save()) {
                $this->render('activation_successful');
            } else {
                // I get this error
                $this->set('status', 'Save error message');
                $this->set('user_data', $user);
                $this->render('activation_fail');
            }
            debug($this->User->invalidFields());

        } else {
            $this->set('status', 'Account not found for this key');
            $this->render('activation_fail');
        }
    } else {
        $this->set('status', 'Empty key');
        $this->render('activation_fail');
    }
}

当我尝试操作 test.com/users/activate/hashedkey 时,我得到带有 Save 的 activation_fail 模板页面错误消息消息。

如果我 printr $user var,我可以从 cake 的 find 方法中获取正确的用户。

我哪里错了?

I'm trying to load, edit and save a record with CakePHP 2.0 but I get a generic error during the save method that don't help me to understand where is the problem.

if I try with debug($this->User->invalidFields()); I get an empty array, but I get false from $this->User->save() condition.

Here is the controller action where I get the error:

public function activate ($code = false) {
    if (!empty ($code)) {

        // if I printr $user I get the right user
        $user = $this->User->find('first', array('activation_key' => $code));

        if (!empty($user)) {
            $this->User->set(array (
                'activation_key' => null,
                'active' => 1
            ));

            if ($this->User->save()) {
                $this->render('activation_successful');
            } else {
                // I get this error
                $this->set('status', 'Save error message');
                $this->set('user_data', $user);
                $this->render('activation_fail');
            }
            debug($this->User->invalidFields());

        } else {
            $this->set('status', 'Account not found for this key');
            $this->render('activation_fail');
        }
    } else {
        $this->set('status', 'Empty key');
        $this->render('activation_fail');
    }
}

When I try the action test.com/users/activate/hashedkey I get the activation_fail template page with Save error message message.

If I printr the $user var I get the right user from cake's find method.

Where I'm wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

想挽留2024-12-22 11:47:33

我认为问题可能出在您查询用户记录的方式上。当您执行此操作时:

$user = $this->User->find('first', array('activation_key' => $code));

变量 $user 将以数组形式填充用户记录。您检查以确保它不为空,然后继续; 但是问题是$this->User尚未填充。我想如果你尝试 debug($this->User->id) 它会是空的。 read() 方法 的工作原理你的想法。

您可以尝试使用 $user 数组中的 ID 首先设置模型 ID,如下所示:

if (!empty($user)) {
    $this->User->id = $user['User']['id']; // ensure the Model has the ID to use
    $this->User->set(array (
        'activation_key' => null,
        'active' => 1
    ));
    if ($this->User->save()) {
    ...

编辑: 另一种可能的方法是使用 $user 数组而不是修改当前模型。您说如果您debug($user),您会得到一个有效的用户,所以如果这是真的,您可以执行如下操作:

if (!empty($user)) {
    $user['User']['activation_key'] = null;
    $user['User']['active'] = 1;
    if ($this->User->save($user)) {
    ...

此方法的工作方式与从接收表单数据相同$this->request->data,并在 保存您的本书的数据部分。

我很好奇你的设置是否有其他部分妨碍了。您的应用程序的其他部分可以正确写入数据库吗?您还应该检查以确保没有验证错误,例如他们的示例:

<?php
if ($this->Recipe->save($this->request->data)) {
    // handle the success.
}
debug($this->Recipe->validationErrors);

I think the problem may be in the way you're querying for the User record. When you do this:

$user = $this->User->find('first', array('activation_key' => $code));

The variable $user is populated with the User record as an array. You check to ensure it's not empty, then proceed; but the problem is that $this->User hasn't been populated. I think if you tried debug($this->User->id) it would be empty. The read() method works the way you're thinking.

You could try using the ID from that $user array to set the Model ID first, like so:

if (!empty($user)) {
    $this->User->id = $user['User']['id']; // ensure the Model has the ID to use
    $this->User->set(array (
        'activation_key' => null,
        'active' => 1
    ));
    if ($this->User->save()) {
    ...

Edit: Well another possible approach is to use the $user array instead of modifying the current model. You said that you get back a valid user if you debug($user), so if that's true you can do something like this:

if (!empty($user)) {
    $user['User']['activation_key'] = null;
    $user['User']['active'] = 1;
    if ($this->User->save($user)) {
    ...

This method works in the same way as receiving form data from $this->request->data, and is described on the Saving Your Data part of the book.

I'm curious though if there's another part of your setup that's getting in the way. Can other parts of your app write to the database properly? You should also check to make sure you aren't having validation errors, like their example:

<?php
if ($this->Recipe->save($this->request->data)) {
    // handle the success.
}
debug($this->Recipe->validationErrors);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文