CakePHP奇怪保存所有问题。返回true,但在数据库中没有

时间:2010-11-02 16:35:04

标签: php mysql cakephp save has-many

我有两个型号Statement - > hasMany - > LineItem

我在$this->saveAll()方法之一中将以下数组传递给Statement

$data = array(
    'Statement' => array(
        'employee_id' => 1
    ),
    'LineItem' => array(
        0 => array('job_id' => 1),
        1 => array('job_id' => 9),
        2 => array('job_id' => 12),
    )
);

$this->saveAll($data) == true,但是当我检查数据库中的statements表时,它是空的。奇怪的一点? auto_increment id列已增加1.这也反映在$this->Statement->id与表的当前auto_increment匹配的事实上。

以上所有情况也适用于line_items表,只有auto_increment增加了3。

更新

使用$this->save($data)时,语句正确保存(当然,没有LineItem条记录)。那么,saveAll有什么用呢?

更新

所以,我认为问题出在我的控制器或模型中。我肯定会有一些愚蠢的 doh!时刻。这是我的控制器和我的模型中的代码(为简洁起见,删除了注释和不相关的代码)。

首先,模型:

class Statement extends AppModel {
    public $belongsTo = array('CompanyStatement', 'Employee');
    public $hasMany = array('LineItem');
    public $name = 'Statement';

    public function build_statement($data = NULL) {
        if (!empty($data)) {
            if ($this->saveAll($data)) {
                $result = array(
                    'message' => 'Statement was saved.',
                    'element' => 'flash_success',
                    'success' => TRUE
                );
            } else {
                $result = array(
                    'message' => 'There was a problem saving.',
                    'element' => 'flash_error',
                    'success' => FALSE
                );
            }
        } else {
            $result = array(
                'message' => 'No data was received.',
                'element' => 'flash_error',
                'success' => FALSE
            );
        }
        return $result;
    }
}

控制器:

 class StatementsController extends AppController {
    public $name = 'Statements';

    public function create_new_statement() {
        $result = $this->Statement->build_statement($this->data);
        $this->Session->setFlash($result['message'], $result['element']);
        $this->redirect(array(
            'controller' => 'statements',
            'action' => 'edit',
            $this->Statement->id
        ));
    }
    public function edit($id = NULL) {
        $this->set('statement' => $this->Statement->findById($id));
    }
 }

LineItem模型非常简洁:

class LineItem extends AppModel {
    public $name = 'LineItem';
    public $belongsTo = array('Statement', 'Job');
}

到达某处

我关闭了控制器方法中的重定向,并查看了SQL转储:

Warning (512): SQL Error: 1452: Cannot add or update a child row:
a foreign key constraint fails (`database`.`line_items`,
CONSTRAINT `line_items_ibfk_1`
FOREIGN KEY (`statement_id`) REFERENCES `statements` (`id`)
ON DELETE CASCADE ON UPDATE NO ACTION)

[CORE\cake\libs\model\datasources\dbo_source.php, line 681]

Query: INSERT INTO `line_items` (`job_id`, `modified`, `created`) VALUES (1, '2010-11-02 15:22:39', '2010-11-02 15:22:39') 
Query: INSERT INTO `line_items` (`job_id`, `modified`, `created`) VALUES (9, '2010-11-02 15:22:39', '2010-11-02 15:22:39') 
Query: INSERT INTO `line_items` (`job_id`, `modified`, `created`) VALUES (10, '2010-11-02 15:22:39', '2010-11-02 15:22:39')  

正在回滚交易。

那么为什么在使用saveAll时,statement_id为什么不在事务中添加到查询中?

3 个答案:

答案 0 :(得分:2)

当我忘记提交交易时,偶尔会发生这种情况。你有没有自动提交?你在使用交易吗?你忘了提交或告诉你需要提交的cakePHP吗?

答案 1 :(得分:1)

默认情况下,Cake会期望line_items上的外键为statement_id。您尚未显示表格结构。在line_items上实际上是一个列statement_id吗?

答案 2 :(得分:0)

$this->saveAll();

无效,请尝试:

$this->Statement->saveAll();
/* or */
$this->LineItem->saveAll();
相关问题