使用 cakephp 保存数据不起作用
我正在尝试使用 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 技术交流群。

我认为问题可能出在您查询用户记录的方式上。当您执行此操作时:
变量
$user
将以数组形式填充用户记录。您检查以确保它不为空,然后继续; 但是问题是$this->User
尚未填充。我想如果你尝试debug($this->User->id)
它会是空的。 read() 方法 的工作原理你的想法。您可以尝试使用
$user
数组中的 ID 首先设置模型 ID,如下所示:编辑: 另一种可能的方法是使用
$user
数组而不是修改当前模型。您说如果您debug($user)
,您会得到一个有效的用户,所以如果这是真的,您可以执行如下操作:此方法的工作方式与从
接收表单数据相同$this->request->data
,并在 保存您的本书的数据部分。我很好奇你的设置是否有其他部分妨碍了。您的应用程序的其他部分可以正确写入数据库吗?您还应该检查以确保没有验证错误,例如他们的示例:
I think the problem may be in the way you're querying for the User record. When you do this:
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 trieddebug($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: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 youdebug($user)
, so if that's true you can do something like this: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: