我正在尝试使用我的CakePHP应用程序使用DerEuromark的密码行为但是无法让它工作。我按照安装说明(http://www.dereuromark.de/2011/08/25/working-with-passwords-in-cakephp/),修改了我的控制器和视图,但我一直收到错误消息,指出我的BeforeValidate
和BeforeSave
与行为不兼容 - 当然,行为不起作用。
我知道我需要在我的模型中正确设置这两个,但我不知道它们应该是什么样子 - 说明并没有涵盖这一点。
基本的,香草BeforeValidate
和BeforeSave
需要什么样才能处理这种行为?
在我的用户控制器下:
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']);
}
我的BeforeValidate
需要兼容的BeforeSave
和user.php
的密码行为: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()) {
}
}
答案 0 :(得分:1)
错误已经告诉您已经非常准确地要做的事情:使行为的方法签名与ModelBehavior方法签名的签名匹配。
看起来插件未更新以反映最新CakePHP版本中的更改。选项数组已添加。
答案 1 :(得分:1)
确保方法签名匹配。方法的参数必须与父类'方法的参数相同。
您可能已在用户模型中忘记了它们。
如果您确实使用了发布链接的代码,则方法签名是正确的。也许您使用的是旧版本或用户模型中存在错误。
您还必须更新$actsAs
属性才能使用此行为。
public $actsAs = array(
'Acl' => array('type' => 'requester'),
'Passwordable',
);
此外,beforeSave
和beforeValidate
必须返回true才能继续执行保存过程。否则,它会中止。请参阅http://book.cakephp.org/2.0/en/models/callback-methods.html。
答案 2 :(得分:0)
为你的两个方法(beforeValidate,beforeSave)执行此操作
public function beforeValidate ($options=array()) {
...
...
}
//or try this if it didn't work
public function beforeValidate (Model $model, $options=array()) {
...
...
}