0

我正在尝试在我的 CakePHP 应用程序中使用 DerEuromark 的 Passwordable 行为,但无法使其正常工作。我按照安装说明(http://www.dereuromark.de/2011/08/25/working-with-passwords-in-cakephp/),修改了我的控制器和视图,但我一直收到一个错误,说我的BeforeValidateBeforeSave与行为不兼容 - 当然,行为不起作用。

我知道我需要在我的模型中正确设置这两个,但我不知道它们应该是什么样子 - 说明没有涵盖这一点。

处理这种行为的基本、普通BeforeValidateBeforeSave需要是什么样的?

在我的用户控制器下:

public function register() {
if ($this->request->is('post') || $this->request->is('put')) {
    $this->User->Behaviors->attach('Tools.Passwordable');
    if ($this->User->save($this->request->data, true, array('username', 'name', 'email', 'pwd', 'pwd_repeat', 'group_id'))) {
    $this->Session->setFlash(__('The user has been saved'), 'flash/success');
            $this->redirect(array('action' => 'index'));
} else {
            $this->Session->setFlash(__('The user could not be saved. Please, try again.'), 'flash/error');
        }
   unset($this->request->data['User']['pwd']);
    unset($this->request->data['User']['pwd_repeat']);
}

BeforeValidateBeforeSaveuser.php需要兼容的 Passwordable 行为: https ://github.com/dereuromark/tools/blob/master/Model/Behavior/PasswordableBehavior.php

错误:

Strict (2048): Declaration of PasswordableBehavior::beforeValidate() should be compatible with ModelBehavior::beforeValidate(Model $model, $options = Array) [APP/Plugin/Tools/Model/Behavior/PasswordableBehavior.php, line 338]
Strict (2048): Declaration of PasswordableBehavior::beforeSave() should be compatible with ModelBehavior::beforeSave(Model $model, $options = Array) [APP/Plugin/Tools/Model/Behavior/PasswordableBehavior.php, line 338]

编辑:用户模型:

<?php
App::uses('AppModel', 'Model');
App::uses('AuthComponent', 'Controller/Component');
App::uses('PasswordableBehavior', 'Tools.Model/Behavior');
/**
 * User Model
 *
 * @property Group $Group
 * @property Post $Post
 */
class User extends AppModel {

    //simplified per-group only permissions- tell ACL to skip checking user AROs and only check group AROs
    public function bindNode($user) {
    return array('model' => 'Group', 'foreign_key' => $user['User']['group_id']);
}

/**
 * Validation rules
 *
 * @var array
 */
public $validate = array(
    'username' => array(
        'notEmpty' => array(
            'rule' => array('notEmpty'),
            //'message' => 'Your custom message here',
            //'allowEmpty' => false,
            //'required' => false,
                //'last' => false, // Stop validation after this rule
                //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),

                    'username' => array(
            'rule' => 'isUnique',
            'required' => true,
            'allowEmpty' => false,
            'on' => 'create',
            'last' => false,
            'message' => 'That username has already been taken'
    ),
        ),


        'email' => array(
            'email' => array(
                'rule' => array('email'),
                //'message' => 'Your custom message here',
                //'allowEmpty' => false,
                //'required' => false,
                //'last' => false, // Stop validation after this rule
                //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),
        'password' => array(
            'notEmpty' => array(
                'rule' => array('notEmpty'),
                //'message' => 'Your custom message here',
                //'allowEmpty' => false,
                //'required' => false,
                //'last' => false, // Stop validation after this rule
                //'on' => 'create', // Limit validation to 'create' or     'update' operations
            ),
        ),
        'group_id' => array(
            'numeric' => array(
                'rule' => array('numeric'),
                //'message' => 'Your custom message here',
                //'allowEmpty' => false,
                //'required' => false,
                //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
        ),
    ),
);

//The Associations below have been created with all possible keys, those that are         not needed can be removed

/**
 * belongsTo associations
 *
 * @var array
 */    
    public $belongsTo = array(
        'Group' => array(
            'className' => 'Group',
            'foreignKey' => 'group_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        )
    );

    public $actsAs = array('Acl' => array('type' => 'requester'));

    public function parentNode() {
        if (!$this->id && empty($this->data)) {
            return null;
        }
        if (isset($this->data['User']['group_id'])) {
            $groupId = $this->data['User']['group_id'];
        } else {
            $groupId = $this->field('group_id');
        }
        if (!$groupId) {
            return null;
        } else {
            return array('Group' => array('id' => $groupId));
        }
    }

/**
 * hasMany associations
 *
 * @var array
 */
    public $hasMany = array(
        'Post' => array(
            'className' => 'Post',
            'foreignKey' => 'user_id',
            'dependent' => false,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        )
    );

    public function beforeValidate($options = array()) {

    }   


public function beforeSave($options = array()) {

} 

}
4

3 回答 3

1

该错误已经非常准确地告诉您必须做什么:使行为的方法签名与 ModelBehavior 方法签名的签名相匹配。

看起来插件没有更新以反映最新 CakePHP 版本中的变化。添加了选项数组。

于 2014-01-05T23:17:23.653 回答
1

确保方法签名匹配。方法的参数必须与父类方法的参数相同。

您可能在您的用户模型中忘记了它们。

如果您真的在使用您发布链接的代码,则方法签名是正确的。也许您使用的是旧版本或者您的用户模型有错误。

您还必须更新$actsAs属性才能使用该行为。

public $actsAs = array(
    'Acl' => array('type' => 'requester'),
    'Passwordable',
);

此外,beforeSave并且beforeValidate必须返回 true 才能继续进行保存过程。否则,它将中止。请参阅http://book.cakephp.org/2.0/en/models/callback-methods.html

于 2014-01-05T23:18:26.480 回答
0

在你 PasswordableBehavior 为你的两种方法(beforeValidate , beforeSave )执行此操作

public function beforeValidate ($options=array()) {
   ...
   ...
}

//or try this if it didn't work
public function beforeValidate (Model $model, $options=array()) {
   ...
   ...
}
于 2014-03-20T12:32:00.850 回答