提问者:小点点

在Cakephp中安装可记录行为时未定义变量


我想做的是:

我想在Cakephp中安装Alkemann的LogableBe的行为。

我得到的错误是:

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

错误:

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

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

LogableBewayor::afterSave()-APP\Model\行为\LogableBehavior.php,第431行ObjectCollection::触发器()-核心\蛋糕\实用程序\ObjectCollection.php,第130行call_user_func-[内部],行??CakeEventManager::dispatch()-核心\蛋糕\事件\CakeEventManager.php,行246模型::保存()-核心\蛋糕\模型\Model.php,行1755模型::保存字段()-核心\蛋糕\模型\Model.php,第1582行ChantiersController::inPlaceDateUpdate()-APP\Controller\ChantiersController.php,第370行反射方法d::调用Args()-[内部],行??控制器::调用操作()-核心\蛋糕\控制器\Controller.php,行485 Dispatcher::_invoke()-核心\蛋糕\路由\Dispatcher.php,行186 Dispatcher::dispatch()-核心\蛋糕\路由\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');

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

  • Github

一无所获。

让我们深入了解代码:

这是代码。

该错误涉及错误行431:

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

似乎$logData尚未声明。

此行在函数中,afterSave(

这个函数是测试的序列,其中431行被称为a的最末端。$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
    }

我的问题:

理想情况下,我希望了解此Behabior的人提供一些关于如何修复错误的建议,但是任何关于如何调试错误的通用方法都将被乐意接受。

编辑:我使用CakePHP2.2。0


共2个答案

匿名用户

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

它使用自己的日志表/模型。但除此之外,它应该是相当直接的。日志表的sql位于Config/sql/log中。sql但可能与您的相同。

匿名用户

这是一个小缺陷,你可以向剧本的制作者提出。

为了防止出现通知,快速修复方法是在第431行之前添加以下内容:

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

或者在函数声明后添加$logData,如下所示:

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

请记住,一般来说,通知在实时网站上不可见,但修复它们是一个很好的做法:Stackoverflow回答