我已经在许多论坛上搜索了我真正奇怪的问题,但我仍然无法弄清楚在我的保存过程中出了什么问题......问题:Cake 说,我的数据已保存,创建了一个自动增量 ID 但没有记录存储在数据库中。
环境
我有一个 cake-1.3.13 应用程序运行了一段时间,现在需要添加另一个数据库表,这当然与其他表相关。我的问题是为 habtm-relation 表保存记录,它看起来像这样:
CREATE TABLE IF NOT EXISTS `employees_projects_rejectreasons` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`employees_project_id` int(10) unsigned NOT NULL,
`rejectreason_id` int(10) unsigned NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `employees_project_id` (`employees_project_id`,`rejectreason_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=6;
我仅使用基本验证标准构建了简单模型。
<?php
class EmployeesProjectsRejectreason extends AppModel {
var $name = 'EmployeesProjectsRejectreason';
var $validate = array(
'employees_project_id' => array(
'numeric' => array(
'rule' => array('numeric'),
//'message' => 'Your custom message here',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
'rejectreason_id' => array(
'numeric' => array(
'rule' => array('numeric'),
//'message' => 'Your custom message here',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
);
//The Associations below have been created with all possible keys, those that are not needed can be removed
var $belongsTo = array(
'EmployeesProject' => array(
'className' => 'EmployeesProject',
'foreignKey' => 'employees_project_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'Rejectreason' => array(
'className' => 'Rejectreason',
'foreignKey' => 'rejectreason_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
我为 Rejectreasons 和 EmployeesProjects 创建了几条记录,所以我在数据库中有一些有效的条目。现在我想通过在给定的 employees_projects_rejectreasons 表中创建新记录将它们链接在一起。我尝试从另一个 Controller (EmployeesProjectsController)执行此操作。这是我最近一次保存数据的尝试:
$this->EmployeesProject->EmployeesProjectsRejectreason->create();
$eprData = array(
'EmployeesProjectsRejectreason' => array(
'employees_project_id' => (int)$id,
'rejectreason_id' => (int)$rrId
)
);
if($this->EmployeesProject->EmployeesProjectsRejectreason->save($eprData)) {
debug('successfully saved EPR with ID '.$this->EmployeesProject->EmployeesProjectsRejectreason->__insertID);
} else {
debug('could not save EPR with employees_project_id='.$id.' and rejectreason_id='.$rrId);
}
现在发生了什么
在我尝试保存记录后,我的调试为我提供了以下成功报告:
successfully saved EPR with ID 4
因此 save() 调用返回 true,mySQL 的 auto_increment 函数创建了一个新 ID。到目前为止,一切都很好。但是当我检查我的数据库时,没有创建记录。但是auto_increment_counter加了1,好像存了一条记录,其实没有。
使用调试级别 2 运行应用程序,我可以看到从 cake 生成的 SQL 语句,这对我来说非常好:
INSERT INTO `employees_projects_rejectreasons` (`employees_project_id`, `rejectreason_id`) VALUES (3, 3)
如果我直接在 sql server 上运行这条语句,记录就会正确插入。
我已经尝试过的
我已经尝试过保存过程的不同方法。我尝试使用 setter 而不是数据数组:
$this->EmployeesProject->EmployeesProjectsRejectreason->set('employees_project_id', $id);
也一样,但没什么区别。在我在 EmployeesProjectsRejectreason-Model 中编写自定义保存方法后,从 Controller 调用它,但它总是产生相同的结果。
我试过了
- 删除模型缓存
- 重新启动服务器实例和服务器本身
- 删除表并重新创建
- 在模型中禁用验证
- 删除唯一外键索引
- 使用硬编码和现有 ID 作为外键保存
一些更奇怪的行为
在我的 Controller 代码中使用硬编码 ID 进行的最后一次测试让我遇到了更多谜题:如果我尝试存储现有的 foreign_key-ID,数据不会像以前那样保存。但是如果两个 ID 都是硬编码的并且不存在(我使用了发明的 ID 345 和 567,它们肯定不存在于数据库中)最终插入了一条记录!
此外,我为新表搭建了模型、 View 和 Controller 脚手架。当我运行脚手架 View “myApp/employees_projects_rejectreasons/add”并添加新记录时,一切正常。
我只是无法保存来自其他 Controller 的记录。由于我已经很头疼,解决这个问题,我非常感谢任何解决方案的提示!!
提前谢谢大家!
最佳答案
我终于找到了解决问题的方法。我仍然不知道,为什么之前的保存代码不起作用,但这是我更改代码以使其起作用的方法:
在我的表单中,数据数组采用以下格式:
Array
(
[EmployeesProject] => Array
(
[id] => 10
[user_id] => 0
[additional_information] => some comment text
[state] => absage
[Rejectreason] => Array
(
[0] => 1
[1] => 8
)
)
)
我搜索了一些解决方案,可以通过一次调用直接在cakePHP 中保存habtm 关系,但这在cake-1.3 中似乎是不可能的。所以我在我的 EmployeesProjectController 中创建了这个非常简单的保存例程,它对我来说非常好:
if (!empty($this->data)) {
if ($this->EmployeesProject->save($this->data)) {
if(array_key_exists('Rejectreason', $this->data['EmployeesProject'])) {
foreach($this->data['EmployeesProject']['Rejectreason'] as $key => $rrId) {
$this->EmployeesProject->EmployeesProjectsRejectreason->create();
$this->EmployeesProject->EmployeesProjectsRejectreason->set('rejectreason_id', $rrId);
$this->EmployeesProject->EmployeesProjectsRejectreason->set('employees_project_id', $this->data['EmployeesProject']['id']);
if($this->EmployeesProject->EmployeesProjectsRejectreason->save()) {
}
}
}
}
}
感谢@Yoggi 支持我解决这个问题!
关于mysql - CakePHP 1.3 未保存到数据库,但 sql 语句正确且 insertID 正确增加,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19093200/