save() 返回 false,但在 CakePHP 中没有错误
我的调试值设置为 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
已正确传递到函数中(并填充了合法数据)。 email
和 password
是 User
模型中字段的名称。
我还确认在 $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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
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.这可能会为您提供所需的信息(当然,假设由于数据无效而没有保存):
This will probably give you the info you need (assuming it's not saving because of invalid data, of course):
您在模型或应用程序模型中是否有
beforeValidate()
或beforeSave()
方法?如果是这样,他们会返回 true 吗?如果失败,请使用调试器,在 IDE 中的 cake/libs/models/model.phpsave()
方法顶部设置一个断点,并逐步执行代码,直到返回 false。如果添加die('here');
调用失败。Have you got a
beforeValidate()
orbeforeSave()
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.phpsave()
method and step through the code until it returns false. Failing that adddie('here');
calls.试试这个:
Try this:
@cakePHP 3.6及更高版本:默认情况下,请求数据将在转换为实体之前进行验证。如果任何验证规则失败,返回的实体将包含错误。可以通过 getErrors() 方法读取。
有错误的字段不会出现在返回的实体中:
假设您有一个实体,
如果您想在转换请求数据时禁用验证,请将 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
If you’d like to disable validation when converting request data, set the validate option to false:
Ref: https://book.cakephp.org/3/en/orm/validation.html
请务必检查您的表:
ID
是否启用了自动增量?id
是您的主键吗?auto_increment 问题害死了我。
简单的检查方法:如果任何行的 ID = 0,则 auto_increment 可能已禁用。
Make sure to check your tables:
ID
have auto increment enabled?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.
蛋糕PHP 3.6
CakePHP 3.6