【问题标题】:Certain Database Table fields not saving using cakePHP某些数据库表字段未使用 cakePHP 保存
【发布时间】:2025-12-18 20:30:01
【问题描述】:

我在订阅数据库表中添加了两个新字段,即first_namelast_name 到表subscriptions

但是每当我从表单返回值并尝试保存它们时,它就不会保存到数据库中?

我的代码如下

$this->data['Subscription']['id'] = $subscription['Subscription']['id']; //To get the primary key
$this->data['Subscription']['first_name']; //Echoes out the name I entered
$this->data['Subscription']['last_name']; //Echoes out the surname I entered

然后我打电话给$this->Subscription->save();,它保存了除了我新添加到表中的字段之外的所有其他字段,我尝试了 $this->Subscription->set($this->data['Subscription']);也没有什么想保存的?

我不确定我在这里做错了什么?任何帮助将不胜感激!!!!

【问题讨论】:

  • 我看不到您将值分配给 first_name 和 last_name 的位置。您可以发布该代码吗?尝试在保存之前使用debug($this->data); 以查看该数组中的实际内容。
  • 您可以发布输入数据的部分视图吗?您是否尝试过查看 this->data 是否确实有 first_name 和 last_name?

标签: cakephp


【解决方案1】:

假设您使用 debug 0 运行应用程序,您需要删除 /tmp 中的缓存,因为数据库模式由 CakePHP 缓存。

【讨论】:

  • 是的,这绝对是问题所在。
  • 谢谢。很高兴我终于找到了答案 =)
【解决方案2】:

根据您的描述,您忘记添加$this->要保存的数据,您必须像这样保存数据:

$this->Subscription->save( $this->data );

【讨论】: