CakePHP 1.3 $这 -> save() 不执行查询

发布于 2024-11-15 00:38:38 字数 1392 浏览 5 评论 0原文

我正在尝试使用 CakePHP 1.3 将一些数据保存到 MySQL 数据库。数据不是来自表单,但为了模仿它,我将数据放在 $this ->; 中。像这样的数据数组:

$this -> data = Array (
                         'User' => Array (
                                            'last_login' => date ('Y-m-d H:i:s')
                                         )
                      );

这将创建一个看起来与默认 Cake 数据数组完全相同的数组。到目前为止,一切都很好。

接下来,我调用以下函数:

if ($this -> User -> save ($this -> data))
{
   echo $this -> User -> id;
}
else
{
   echo 'Save failed';
}

它总是出现“保存失败”,表明数据未正确保存。我尝试以多种方式对此进行调试:

我检查了我的 User 模型或 AppModel 中是否存在 beforeSave 或 beforeValidate 函数以及它们是否返回 true。 (他们做到了。)

我回应 $this ->会话-> default.ctp 中的 flash ()。它什么也没说。

我回应了 $this -> default.ctp 中的元素 ('sql_dump')。我没有看到任何我期望的查询的迹象,这证实了我对查询不执行而不是失败的期望。

我从我的控制器回显 $this ->验证错误。它是空的,所以验证似乎没问题。

我试图在我的 $this -> 中提供正确的索引用于保存的数据数组,如下所示:

$this -> User -> save ($this -> data['User']);

我检查是否通过 $this ->; 加载了我的用户模型loadModel ('User') (我做到了)。

请注意,我尝试在 AppController 中执行此操作(这就是我手动加载模型的原因)。更具体地说:它位于 AppController 中的 beforeFilter () 函数中。在我的应用程序拥有的每个其他控制器的 beforeFilter () 函数中调用此函数(我检查了这一点)。

目前,我不知道是什么原因导致这种情况。有人对此有什么想法吗?

Using CakePHP 1.3 I'm trying to save some data to a MySQL database. The data doesn't come from a form, but to imitate it I put the data in the $this -> data array like this:

$this -> data = Array (
                         'User' => Array (
                                            'last_login' => date ('Y-m-d H:i:s')
                                         )
                      );

This creates an array that looks exactly like the default Cake data array. So far so good.

Next, I call the following function:

if ($this -> User -> save ($this -> data))
{
   echo $this -> User -> id;
}
else
{
   echo 'Save failed';
}

It always comes up with 'Save failed', showing the data wasn't saved correctly. I tried to debug this in a variety of ways:

I checked whether a beforeSave or beforeValidate function existed in my User model or AppModel and whether these return true. (They did.)

I echoed $this -> Session -> flash () in default.ctp. It doesn't say anything.

I echoed $this -> element ('sql_dump') in default.ctp. I don't see any sign of the query I expect, confirming my expectation of the query not executing instead of just failing.

From my controller I echoed $this -> validationErrors. It's empty so validation seems to be ok.

I tried to provide the correct index in my $this -> data array for the save, like this:

$this -> User -> save ($this -> data['User']);

I checked whether I loaded my User model via $this -> loadModel ('User') (I did).

Note that I try to do this in my AppController (that's why I loaded the model manually). More specifically: it's in the beforeFilter () function in the AppController. This function is called in the beforeFilter () function in every other Controller my application has (I checked this).

At the moment, I don't know what could cause this. Does anyone have an idea about this?

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

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

发布评论

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

评论(2

流心雨 2024-11-22 00:38:38

您在保存之前没有指定用户 ID,因此 CakePHP 不知道将日期保存到哪一行。实际上,您要求 CakePHP 仅保存日期,而不指定位置。我现在离开了我的 webdev 盒子,但我相信这就是您正在寻找的东西。

if( ! isset( $this->User->id ) ) {
    $this->User->create();
}
$this->User->saveField('last_login', date(DATE_ATOM));

请参阅此页面了解更多信息。顺便说一句,如果你想按照原来的方式来做,我认为是这样的:

if( ! isset( $this->User->id ) ) {
        $this->User->create();
        $myId = $this->User->getLastInsertID();
} else {
  $myId = $this->User->id;
}

$this -> data = Array (
      'User' => Array (
      'id' => $myId
      'last_login' => date ('Y-m-d H:i:s')
   )
);

if ($this -> User -> save ($this -> data))
{
   echo $this -> User -> id;
}
else
{
   echo 'Save failed';
}

You're not specifying the User id before you save, so CakePHP has no idea what row to save the date to. In effect, you're asking CakePHP to just save a date, without specifying where. I am away from my webdev box right now, but I believe this is what you're looking for.

if( ! isset( $this->User->id ) ) {
    $this->User->create();
}
$this->User->saveField('last_login', date(DATE_ATOM));

See this page for more information. BTW, if you want to do it the way you were originally doing it, I think this is the way:

if( ! isset( $this->User->id ) ) {
        $this->User->create();
        $myId = $this->User->getLastInsertID();
} else {
  $myId = $this->User->id;
}

$this -> data = Array (
      'User' => Array (
      'id' => $myId
      'last_login' => date ('Y-m-d H:i:s')
   )
);

if ($this -> User -> save ($this -> data))
{
   echo $this -> User -> id;
}
else
{
   echo 'Save failed';
}
め可乐爱微笑 2024-11-22 00:38:38

如果你想更新你的last_login

$this->User->id

$this->User->saveField('last_login', date ('Ymd H:i: s'));


如果保存所有记录,那么您就有这种类型的数组,并且必须有“id”。

您通过会话或无论如何获得的这个“id”

$this ->数据 = 数组 (
'用户' =>大批 (
'id'=> $myId
'最后登录' =>日期 ('Ymd H:i:s')

);

if ($this -> 用户 -> 保存($this -> 数据))
{
回显 $this ->用户-> ID;
}
别的
{
echo '保存失败';
}

if you want to update your last_login

$this->User->id

$this->User->saveField('last_login', date ('Y-m-d H:i:s'));

or
if you save all record then you have this type of array and you must have "id".

this "id" you get by session or anyway

$this -> data = Array (
'User' => Array (
'id' => $myId
'last_login' => date ('Y-m-d H:i:s')
)
);

if ($this -> User -> save ($this -> data))
{
echo $this -> User -> id;
}
else
{
echo 'Save failed';
}

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