Cakephp Callback函数多次调用

时间:2015-04-13 11:11:57

标签: php cakephp callback save after-save

首先是一些背景信息:

用户可以填写一些输入文本框并上传多个图像(在一个上传字段中)。提交表单后,文本字段中的文本将保存在包含$this->XXX->save()的数据库中。此外,数据(特别是来自文件上载的图像)在回调函数afterSave()中处理。到目前为止一切都很好。

现在问题:

似乎多次调用回调函数(每个上传的图像都有1次)。所以问题是,第一次一切都很好,但之后$this->data数组基本上是空的(所有文本字段都是= '')所以回调函数抛出一些异常,因为没有数据。我只用isset()等检查解决了这个问题,但我认为这不是解决问题的方法。

所以我的问题是,为什么回调函数被多次调用,我该如何防止呢?

修改 遵循一些相关的代码片段:

控制器:

      if ($this->request->is('post') || $this->request->is('put')) {

        if($aid==null) $this->Article->create();
        if ($this->Article->save($this->data)) {
            $this->Session->setFlash(__('The article has been saved'), 'flash_custom');
            return $this->redirect(array('controller' => 'articles', 'action' => 'view', $this->id));
        } ...

型号:

我认为大部分内容与此无关。一般情况下,我将数据发送到其他模型,其中这些数据保存在其他数据库表中,如图所示

 if ($this->data['Article']['picture']) {
   foreach ($this->data['Article']['picture'] as $picture) {
       if (is_uploaded_file($picture['tmp_name']) && (substr($picture['type'], 0, 5) == "image") && $numberOfPictures < $pictures_allowed) {
           $this->Picture->addPicture($picture, $this->id);
       }
       $numberOfPictures++;
   }

错误转储:

我认为$this->data数组的转储在这里非常有趣(下面的解释)

array(
'Article' => array(
    'pictures' => '0',
    'title' => 'Error Test',
    'abstract' => 'Test abstract',
    'body' => 'Testtext',
    'picture' => array(
        (int) 0 => array(
            [maximum depth reached]
        ),
        (int) 1 => array(
            [maximum depth reached]
        ),
        (int) 2 => array(
            [maximum depth reached]
        )
    ),
    'modified' => '2015-04-13 22:00:09',
    'created' => '2015-04-13 22:00:09',
    'id' => '294'
))


Notice (8): Undefined index: title [APP/Model/Article.php, line 723]
Notice (8): Undefined index: body [APP/Model/Article.php, line 724]
Notice (8): Undefined index: abstract [APP/Model/Article.php, line 725]

array(
    'Article' => array(
        'id' => '294',
        'pictures' => (int) 1,
        'modified' => '2015-04-13 22:00:09',
        'title' => '',
        'body' => '',
        'abstract' => '',
    )
)

Notice (8): Undefined index: picture [APP/Model/Article.php, line 777]

Notice (8): Undefined index: title [APP/Model/Article.php, line 723]

Notice (8): Undefined index: body [APP/Model/Article.php, line 724]

Notice (8): Undefined index: abstract [APP/Model/Article.php, line 725]

array(
    'Article' => array(
        'id' => '294',
        'pictures' => (int) 2,
        'modified' => '2015-04-13 22:00:10',
        'title' => '',
        'body' => '',
        'abstract' => '',
    )
)

Notice (8): Undefined index: picture [APP/Model/Article.php, line 777]

Notice (8): Undefined index: title [APP/Model/Article.php, line 723]

Notice (8): Undefined index: body [APP/Model/Article.php, line 724]

Notice (8): Undefined index: abstract [APP/Model/Article.php, line 725]

array(
    'Article' => array(
        'id' => '294',
        'pictures' => (int) 3,
        'modified' => '2015-04-13 22:00:10',
        'title' => '',
        'body' => '',
        'abstract' => '',
    )
)

Notice (8): Undefined index: picture [APP/Model/Article.php, line 777]

说明:

我刚刚转储$this->data一次,但就像你看到我上传了3张图片一样,我收到了3次afterSave调用,因此有3次$this->data转储。第一次一切都很好,但之后会抛出一些警告,因为阵列不再被填充。

我希望这些额外信息可以帮助您

1 个答案:

答案 0 :(得分:1)

如果您通过相同模型的aftersave回调调用Model :: save,那么它将是递归保存,因为aftersave将调用save,这将再次触发aftersave ..

禁用您在aftersave中使用的保存方法中的回调

$this->save($data, array('callbacks' => false));

Documentation