调试save()返回false CakePHP 3.0

问题描述 投票:6回答:3

我有一些代码在途中遇到了一些问题,我在调试它时遇到了麻烦。

这是它的简化版本。

$data = $this->request->data;

$form = $this->Forms->get($data['id'], [
    'contain' => ['FieldsForms' => ['data']
    ]
]);

$form = $this->Forms->patchEntity($form, $data,
    ['associated' => [
        'FieldsForms.Data',

    ]
]);

if ($this->Forms->save($form)) {
    // sunshine and rainbows
} else {
    // wailing and gnashing of teeth
}

我没有任何错误就嗷嗷嗷嗷嗷嗷嗷嗷嗷嗷嗷嗷嗷嗷嗷嗷嗷嗷嗷嗷嗷嗷嗷嗷嗷嗷嗷嗷嗷嗷嗷嗷嗷呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜呜

验证错误是空的。

保存返回false - 任何关于如何调试它的建议都可以保存我留下的理智。

谢谢!

debugging cakephp save cakephp-3.0
3个回答
15
投票

问题原来是数据,正如预期的那样,但由于保存返回错误且数据非常大,因此无法立即看到。

我首先制作了问题数据的一个子集,显示了相同的行为,然后根据ndm的建议,更改了保存函数的ORM / Table.php代码,如下所示,以便能够看到问题所在:

$x = $entity->errors();
        if ($x) {
            debug($entity);
            debug($x);
        // if ($entity->errors()) {
            return false;
        }

这样我就可以看到发生了什么,然后继续修复数据。


1
投票

不确定早期的答案是否基于旧版本,但在最新的cakephp版本(3.4)中,您可以直接从控制器中的$ entity检索错误。 errors数组包含失败的每个实体字段,其子数组的验证失败。

<?php
// In Articles Controller
...

public function add(){
...
  if ($this->Articles->save($article)) {
    $this->Flash->success(__('The Article has been saved.'));
    return $this->redirect(['action' => 'index']);
  } else {
    Log::Debug($article->errors());
  }

0
投票

而是修改核心Cake代码,你可以这样做:

if ($this->Forms->save($form)) {
    // sunshine and rainbows
} else {
    //you know now what fail
    $andTheErrorsAre = $entity->getErrors();
}
© www.soinside.com 2019 - 2024. All rights reserved.