save() 返回 false,但在 CakePHP 中没有错误

发布于 2024-08-22 03:52:02 字数 816 浏览 8 评论 0原文

我的调试值设置为 2,它显示除我需要的查询之外的所有查询。

我有一个 Items 控制器方法,它在 User 模型中调用此方法(Item ownsTo User):

function add_basic($email, $password) {
    $this->create();

    $this->set(array(
        'email' => $email,
        'password' => $password
    ));

    if($this->save()) {
        return $this->id;
    }
    else {
        return false;
    }
}

我已确认 $ email$password 已正确传递到函数中(并填充了合法数据)。 emailpasswordUser 模型中字段的名称。

我还确认在 $this->save() 上它返回 false,但是当我查看发生这种情况的页面时,查询不会被打印在调试,并且没有抛出任何错误,所以我不知道出了什么问题。

关于如何查看错误或为什么查询似乎没有被执行的任何想法?

这很奇怪,因为在此之后,我有另一个模型以完全相同的方式将数据保存到其中,它顺利进行。

My debug value is set to 2, and it's displaying all the queries, except the one I need.

I have an Items controller method that is calling this method in the User model (Item belongsTo User):

function add_basic($email, $password) {
    $this->create();

    $this->set(array(
        'email' => $email,
        'password' => $password
    ));

    if($this->save()) {
        return $this->id;
    }
    else {
        return false;
    }
}

I have confirmed that $email and $password are being passed into the function correctly (and are populated with legit data). email and password are the names of the fields in the User model.

I have also confirmed that on $this->save() it is returning false, but when I view the page where this occurs, the query is not being printed in the debug, and there is no error being thrown, so I have no idea whats going wrong.

Any ideas on how I can see the error, or why the query doesn't seem to be getting executed?

It's weird, cause right after this, I have another model saving data to it in the exact same fashion, it goes off without a hitch.

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

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

发布评论

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

评论(7

甜尕妞 2024-08-29 03:52:03

CakePHP 无法报告任何 $this->Model->validationErrors 并且没有其他错误的另一种情况可能是在 $this->request->data 时发生的。并不像 Cake 期望的那样,只是忽略您的数据,不保存,没有验证错误。例如,如果您的数据由 DataTables 提供,您可能会看到以下格式 $this->request->data[0]['Model']['some_field']

但是,$this->Model->save($this->request->data[0]) 可以工作。

The other situation where CakePHP fails to report any $this->Model->validationErrors and no other errors is potentially when $this->request->data isn't as Cake expects and is simply ignoring your data, not saving, no validation errors. For example if your data was provided by DataTables you might see this format $this->request->data[0]['Model']['some_field'].

$this->Model->save($this->request->data[0]) will work however.

尸血腥色 2024-08-29 03:52:02

这可能会为您提供所需的信息(当然,假设由于数据无效而没有保存):

if(!$this->save()){
    debug($this->validationErrors); die();
}

This will probably give you the info you need (assuming it's not saving because of invalid data, of course):

if(!$this->save()){
    debug($this->validationErrors); die();
}
帅气尐潴 2024-08-29 03:52:02

您在模型或应用程序模型中是否有 beforeValidate()beforeSave() 方法?如果是这样,他们会返回 true 吗?如果失败,请使用调试器,在 IDE 中的 cake/libs/models/model.php save() 方法顶部设置一个断点,并逐步执行代码,直到返回 false。如果添加 die('here'); 调用失败。

Have you got a beforeValidate() or beforeSave() method in the model or app model? Ifso, are they returning true? Failing that, use a debugger, set a break point in your IDE at the top of cake/libs/models/model.php save() method and step through the code until it returns false. Failing that add die('here'); calls.

各空 2024-08-29 03:52:02

试试这个:

if ($this->save()) {
    return $this->id;
}
else {
    var_dump($this->invalidFields());
    return false;
}

Try this:

if ($this->save()) {
    return $this->id;
}
else {
    var_dump($this->invalidFields());
    return false;
}
情丝乱 2024-08-29 03:52:02

@cakePHP 3.6及更高版本:默认情况下,请求数据将在转换为实体之前进行验证。如果任何验证规则失败,返回的实体将包含错误。可以通过 getErrors() 方法读取。
有错误的字段不会出现在返回的实体中:

假设您有一个实体,

use App\Model\Entity\Article;

$entity = $this->ModelName->newEntity([
    'id' => 1,
    'title' => 'New Article',
    'created' => new DateTime('now')
]);

$result = $this->ModelName->save($entity);

\Cake\Log\Log::debug($entity->getErrors());

如果您想在转换请求数据时禁用验证,请将 validate 选项设置为 false:

$article = $articles->newEntity(
    $this->request->getData(),
    ['validate' => false]
);

参考:https://book.cakephp.org/3/en/orm/validation.html< /a>

@cakePHP 3.6 and above: By default, the request data will be validated before it is converted into entities. If any validation rules fail, the returned entity will contain errors. It can be read by getErrors() method.
The fields with errors will not be present in the returned entity:

Say, you have an entity

use App\Model\Entity\Article;

$entity = $this->ModelName->newEntity([
    'id' => 1,
    'title' => 'New Article',
    'created' => new DateTime('now')
]);

$result = $this->ModelName->save($entity);

\Cake\Log\Log::debug($entity->getErrors());

If you’d like to disable validation when converting request data, set the validate option to false:

$article = $articles->newEntity(
    $this->request->getData(),
    ['validate' => false]
);

Ref: https://book.cakephp.org/3/en/orm/validation.html

她说她爱他 2024-08-29 03:52:02

请务必检查您的表:

  • ID 是否启用了自动增量?
  • id 是您的主键吗?

auto_increment 问题害死了我。
简单的检查方法:如果任何行的 ID = 0,则 auto_increment 可能已禁用。

Make sure to check your tables:

  • Does ID have auto increment enabled?
  • Is id your primary key?

the auto_increment issues killed me.
Easy way to check: if any of your rows have ID = 0, auto_increment is likely disabled.

岁月苍老的讽刺 2024-08-29 03:52:02

蛋糕PHP 3.6

$entity = $this->Model->newEntity([
    'account_id' => $id,
    'gallery_id' => $gallery_id
]);

$result = $this->Model->save($entity);

print_r($entity->getErrors());

CakePHP 3.6

$entity = $this->Model->newEntity([
    'account_id' => $id,
    'gallery_id' => $gallery_id
]);

$result = $this->Model->save($entity);

print_r($entity->getErrors());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文