使用cakephp保存数据将无法正常工作 [英] Saving data with cakephp won't work
问题描述
我正在尝试加载
,编辑
和保存
一个记录与 CakePHP 2.0
,但在保存
期间,我得到一个通用错误
code>方法,不帮助我了解问题在哪里。
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.
如果我尝试使用 debug($ this-> ; User-> invalidFields());
我得到一个空数组
,但我得到 false
从 $ this-> User-> save()
条件。
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');
}
}
当我尝试动作 test.com/users/activate/hashedkey
我得到 activation_fail
模板页面,保存错误消息
消息。
When I try the action test.com/users/activate/hashedkey
I get the activation_fail
template page with Save error message
message.
如果我 printr
$ user
var我从蛋糕的 find
方法获取正确的用户。
If I printr
the $user
var I get the right user from cake's find
method.
我错了什么?
推荐答案
我认为问题可能在于您查询用户记录的方式。当您这样做:
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));
变量 $ user
填充了用户记录为数组。您检查以确保它不为空,然后继续; 但是问题是,$ code> $ this-> User 尚未填充。我想如果你尝试了 debug($ this-> User-> id)
,它将为空。 read()方法可以使用
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.
您可以尝试使用 $ user
数组中的ID来设置模型ID首先,像这样:
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()) {
...
编辑:另一种可能的方法是使用 $ user
数组,而不是修改当前模型。你说如果你 debug($ user)
,你可以得到一个有效的用户,所以如果这是真的,你可以这样做:
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-> request-> data
接收表单数据的工作方式相同, a href =http://book.cakephp.org/2.0/en/models/saving-your-data.html =nofollow>保存您的数据本书的一部分。
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);
这篇关于使用cakephp保存数据将无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!