CakePHP 奇怪的 saveAll 问题。返回true,但数据库中没有任何内容

发布于 2024-09-30 18:32:45 字数 3669 浏览 5 评论 0原文

我有两个模型,Statement ->有很多 -> 行项目

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

$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),Statement 已正确保存(当然,没有 LineItem 记录)。那么,saveAll 是怎么回事?

更新

所以,我认为问题出在我的控制器或模型中的某个地方。我确信这是一些愚蠢的时刻。这是我的控制器和模型中的代码(为简洁起见,删除了注释和不相关的代码)。

首先,模型:

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');
}

Getting Somewhere

我关闭了控制器方法中的重定向,并查看了 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 没有添加到事务中的查询中呢?

I have two models, Statement -> hasMany -> LineItem.

I'm passing the following array to $this->saveAll() inside one of Statement's methods:

$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, but when I check the statements table in the database, it is empty. The strange bit? The auto_increment id column has been incremented by 1. This is also reflected by the fact that $this->Statement->id matches the current auto_increment of the table.

All of the above is true for the line_items table as well, only the auto_increment has been incremented by 3.

Update

When using $this->save($data), the Statement is saved correctly (of course, without the LineItem records). So, what's up with saveAll?

Update

So, I suppose the problem is somewhere in my controller or model. Some stupid doh! moment I'm sure. Here is the code from my controller and my model (comments and unrelated code removed for brevity).

First, the model:

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;
    }
}

And the controller:

 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));
    }
 }

The LineItem model is very concise:

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

Getting Somewhere

I turned off the redirect in the controller method, and took a look at the SQL dump:

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')  

The transaction is being rolled back.

So why isn't the statement_id being added to the query in the transaction when using saveAll?

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

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

发布评论

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

评论(3

冷月断魂刀 2024-10-07 18:32:45

当我忘记提交事务时,这种情况偶尔会发生在我身上。您是否禁用了自动提交?您使用交易吗?您是否忘记提交或告诉 cakePHP 您需要提交?

This happens to me every once in a while when I forget to commit transactions. Have you disabled autocommit? Are you using transactions? Did you forget to commit or tell cakePHP you needed to commit?

傲性难收 2024-10-07 18:32:45

默认情况下,Cake 期望 line_items 上的外键为 statements_id。您还没有显示表结构。 line_items 上实际上有一个列statement_id 吗?

By default Cake will expect the foreign key on line_items to be statement_id. You haven't shown the table structures. Is there actually a column statement_id on line_items?

后知后觉 2024-10-07 18:32:45
$this->saveAll();

不会工作,而是尝试:

$this->Statement->saveAll();
/* or */
$this->LineItem->saveAll();
$this->saveAll();

won't work, instead try:

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