Model CakePHP中的乐观记录锁定

Model CakePHP中的乐观记录锁定,model,cakephp-2.0,behavior,optimistic-locking,Model,Cakephp 2.0,Behavior,Optimistic Locking,我已经开始使用CakePHP,我正在尝试对我发现不在核心的记录设置一些乐观的锁定。在Apress bookPractical CakePHP项目中,有一个使用“锁定”字段在行为中创建乐观锁定的示例。这是为CakePHP1.2编写的,我正在使用2.0,我还希望使用“修改”字段,而不是单独的锁定字段 我计划做的是查看检索到的记录中的修改日期,然后在beforeValidate函数中将其与数据库中的内容进行比较。如果不同,则其他人可以访问该记录,因此生成异常 我的功能是 function before

我已经开始使用CakePHP,我正在尝试对我发现不在核心的记录设置一些乐观的锁定。在Apress bookPractical CakePHP项目中,有一个使用“锁定”字段在行为中创建乐观锁定的示例。这是为CakePHP1.2编写的,我正在使用2.0,我还希望使用“修改”字段,而不是单独的锁定字段

我计划做的是查看检索到的记录中的修改日期,然后在beforeValidate函数中将其与数据库中的内容进行比较。如果不同,则其他人可以访问该记录,因此生成异常

我的功能是

function beforeValidate(&$model) {
    // First find the record
    if (isset($model->data[$model->name]['id']) ){
        $id = $model->data[$model->name]['id'];
        $table = $model->table;
        $currentRecord = $model->find('all', array('conditions'=>array('id'=>$id)));

        if(!empty($currentRecord)) {
            if($model->data[$model->name]['modified'] != $currentRecord[0][$table]['modified']) {
                $model->validationErrors['modified'] = 'Update conflict, another user has already updated the record. Please list and edit the record again.';
                return false;
            }
        }
    }
    return true;
}
我发现,当我尝试运行此程序时,会出现以下错误

Notice (8): Undefined index: modified [APP/Model/Behavior/MagicFieldsPlusBehavior.php, line 21]
Notice (8): Undefined index: schools [APP/Model/Behavior/MagicFieldsPlusBehavior.php, line 21]
“学校”是表,“修改”字段-是的,它们存在:-)

第21行是

if($model->data[$model->name]['modified'] != $currentRecord[0][$table]['modified']) {
提前感谢您帮助调试新手的错误

布莱恩