0

我正在尝试做的事情:

我想在 Cakephp 中安装Alkemann的 LogableBehavior。

我得到的错误:

当我要进行编辑时,会返回错误:

错误:

注意(8):未定义变量:logData [APP\Model\Behavior\LogableBehavior.php,第 431 行]

标准 cakephp 错误中给出的上下文:

LogableBehavior::afterSave() - APP\Model\Behavior\LogableBehavior.php,第 431 行 ObjectCollection::trigger() - CORE\Cake\Utility\ObjectCollection.php,第 130 行 call_user_func - [内部],行 ?? CakeEventManager::dispatch() - CORE\Cake\Event\CakeEventManager.php,第 246 行 Model::save() - CORE\Cake\Model\Model.php,第 1755 行 Model::saveField() - CORE\Cake\Model \Model.php,第 1582 行 ChantiersController::inPlaceDateUpdate() - APP\Controller\ChantiersController.php,第 370 行 ReflectionMethod::invokeArgs() - [内部],行 ?? Controller::invokeAction() - CORE\Cake\Controller\Controller.php,第 485 行 Dispatcher::_invoke() - CORE\Cake\Routing\Dispatcher.php,第 186 行 Dispatcher::dispatch() - CORE\Cake\Routing \Dispatcher.php,第 161 行 [主] - APP\webroot\index.php,第 92 行

标准 cakephp 错误中给出的代码:

$Model = object(Chantier) {}
$created = false
  $keys = array(
      (int) 0 => 'id',
      (int) 1 => 'Date_de_debut_MOA',
      (int) 2 => 'modified'
)
$diff = array(
    (int) 1 => 'Date_de_debut_MOA',
    (int) 2 => 'modified'
)
$id = '126'

我试图做的事情:

我检查了所有正确完成的安装步骤,根据文档有三个:

    Requires the following to work as intended :
 * 
 * - "Log" model ( empty but for a order variable [created DESC]
 * - "logs" table with these fields required :
 *     - id         [int]           : 
 *     - title      [string]        : automagically filled with the display field of the model that was modified.
 *     - created    [date/datetime] : filled by cake in normal way
 * 
 * - actsAs = array("Logable"); on models that should be logged

在我看来,我做的一切都很好:

我的日志表

    CREATE TABLE IF NOT EXISTS `logs` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `title` varchar(100) NOT NULL,
  `created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `description` varchar(400) NOT NULL,
  PRIMARY KEY (`id`)
  ) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=45 ;

我的日志模型:

   <?php
App::uses('AppModel', 'Model');
class Log extends AppModel {
var $order=array("Log.created"=>"DESC");

}

在 App/Model/Chantier 中:

var $actsAs = array('Containable','Logable');

然后我搜索有类似问题的人:

什么也没找到。

让我们深入研究代码:

这是代码

该错误涉及错误行 431:

        $this->_saveLog($Model, $logData); //line 431

似乎 $logData 尚未声明。

这一行在一个函数中,afterSave(&$Model,$created)

该函数是一系列测试,其中第 431 行称为最后。$logData 有许多声明,但总是嵌套在 if 语句中。

function afterSave(&$Model,$created) {
        
        if (isset($this->settings[$Model->alias]['skip']['add']) && $this->settings[$Model->alias]['skip']['add'] && $created) {
            return true;
        } elseif (isset($this->settings[$Model->alias]['skip']['edit']) && $this->settings[$Model->alias]['skip']['edit'] && !$created) {
            return true;
        }
        $keys = array_keys($Model->data[$Model->alias]);
        $diff = array_diff($keys,$this->settings[$Model->alias]['ignore']);
        if (sizeof($diff) == 0 && empty($Model->logableAction)) {
            return false;
        }
        if ($Model->id) {
            $id = $Model->id;
        } elseif ($Model->insertId) {
            $id = $Model->insertId;
        }       
        if (isset($this->Log->_schema[$this->settings[$Model->alias]['foreignKey']])) {
            $logData['Log'][$this->settings[$Model->alias]['foreignKey']] = $id;
        }
        if (isset($this->Log->_schema['description'])) {        
            $logData['Log']['description'] = $Model->alias.' ';
            if (isset($Model->data[$Model->alias][$Model->displayField]) && $Model->displayField != $Model->primaryKey) {
                $logData['Log']['description'] .= '"'.$Model->data[$Model->alias][$Model->displayField].'" ';
            }

            if ($this->settings[$Model->alias]['description_ids']) {
                $logData['Log']['description'] .= '('.$id.') ';
            }

            if ($created) {
                $logData['Log']['description'] .= __('added',TRUE);
            } else {
                $logData['Log']['description'] .= __('updated',TRUE);   
            }  
        }     
        if (isset($this->Log->_schema['action'])) {                 
            if ($created) {
                $logData['Log']['action'] = 'add';
            } else { 
                $logData['Log']['action'] = 'edit';         
            }  

        }
        if (isset($this->Log->_schema['change'])) {
            $logData['Log']['change'] = '';
            $db_fields = array_keys($Model->_schema);
            $changed_fields = array();
            foreach ($Model->data[$Model->alias] as $key => $value) {
                if (isset($Model->data[$Model->alias][$Model->primaryKey]) && !empty($this->old) && isset($this->old[$Model->alias][$key])) {
                    $old = $this->old[$Model->alias][$key];
                } else {
                    $old = '';
                }
                if ($key != 'modified' 
                    && !in_array($key, $this->settings[$Model->alias]['ignore'])
                    && $value != $old && in_array($key,$db_fields) ) 
                    {
                        if ($this->settings[$Model->alias]['change'] == 'full') {
                            $changed_fields[] = $key . ' ('.$old.') => ('.$value.')';
                        } else if ($this->settings[$Model->alias]['change'] == 'serialize') {
                            $changed_fields[$key] =  array('old'=>$old, 'value'=>$value);
                        } else {
                            $changed_fields[] = $key;   
                        }                   
                    }
            }
            $changes = sizeof($changed_fields);
            if ($changes == 0) {
                return true;
            } 
            if ($this->settings[$Model->alias]['change'] == 'serialize') {
                $logData['Log']['change'] = serialize($changed_fields);
            } else {
                $logData['Log']['change'] = implode(', ',$changed_fields);
            }
            $logData['Log']['changes'] = $changes;      
        }  
        $this->_saveLog($Model, $logData);//line 431
    }

我的问题:

理想情况下,我希望知道此行为的人就如何修复错误提供一些建议,但任何有关如何调试它的通用方法都将被欣然接受。

编辑:我使用 Cakephp 2.2.0

4

2 回答 2

1

你似乎使用了一个非常非常旧的版本。从那时起,它得到了很大的改进。如果您使用的是 cake2.x,这是一个应该是最新的(并且所有测试用例都通过): https ://github.com/dereuromark/tools/blob/2.0/Model/Behavior/LogableBehavior.php

它使用自己的日志表/模型。但除此之外,它应该非常简单。日志表的 sql 在Config / sql / log.sql但可能与您的相同。

于 2012-08-17T09:06:37.113 回答
0

好吧,这是一个小缺陷,您可以向脚本的制作者解决。

防止通知的快速解决方法是在第 431 行之前添加以下内容:

<?php     
     $logData = (!isset($logData) ? array() : $logData );
     $this->_saveLog($Model, $logData);
?>

或者像这样在函数声明之后添加 $logData :

<?php
    function afterSave(&$Model,$created) {
        $logData = array();

请记住,通知通常在实时网站上不可见,但修复它们是一种很好的做法:Stackoverflow answer

于 2012-08-17T08:56:09.300 回答