PHPActiveRecord验证不工作的唯一性
我当前在我的用户模型中遇到以下错误PHPActiveRecord验证不工作的唯一性,php,activerecord,model,php-7,phpactiverecord,Php,Activerecord,Model,Php 7,Phpactiverecord,我当前在我的用户模型中遇到以下错误 Slim Application Error The application could not run because of the following error: Details Type: ActiveRecord\UndefinedPropertyException Message: Undefined property: User->Array in /var/www/public_html/devsite/vendor/php-acti
Slim Application Error
The application could not run because of the following error:
Details
Type: ActiveRecord\UndefinedPropertyException
Message: Undefined property: User->Array in /var/www/public_html/devsite/vendor/php-activerecord/php-activerecord/lib/Model.php on line 521
File: /var/www/public_html/devsite/vendor/php-activerecord/php-activerecord/lib/Model.php
Line: 521
我的模型只有在添加下面的行时才会使程序崩溃。
static $validates_uniqueness_of = array(
'username'
);
如果我删除上面的行,那么程序会再次正常运行。所以我知道这与此有关
根据文件,这确实应该是格式。
()
对库中验证函数的引用如下:
第563行--
我正在ubuntu0.16.04.4上使用PHP版本7.0.15-0
如果这确实是一个bug,有人有任何解决方法吗?公共静态$validates\u university\u of=array( “用户名”
)好的,我现在创建了一个完整的解决方案。欢迎任何能够改进这一点的人 首先创建一个单独的文件,并将其命名为uniquecheck.php 里面放着这个特征码
trait uniquecheck {
//@JA - This function can do single and multi-unique checks.
//@JA - This is programmed to be replaced at a later date when validates_uniqueness_of is fixed (http://www.phpactiverecord.org/projects/main/wiki/Validations#validates_uniqueness_of)
//@JA - EXAMPLES
//SINGLE -- array('name','message' => 'Can't do this')
//MULTIPLE -- array( array('name1','name2'), 'message' => 'can't do this and that together')
//@JA - To be clear multiple does not mean 2 different uniques but a unique on 2 columns. Just use this function twice for 2 separate unique checks.
//@JA - Refer to (https://github.com/jpfuentes2/php-activerecord/issues/336)
public function uniquecheck($rules = array()) {
//@JA - If its an array use the MULTIPLE method
$dirty = $this->dirty_attributes();//@JA - Get list of attributes that have been modified since loading the model
if(is_array($rules[0])){
//@JA - Generate first part of condition string
$uniques = $rules[0];
foreach($uniques as $unique){
$conditionstring .= "$unique = ? AND ";
}
$conditionstring = substr($conditionstring, 0, -5);
$dirtyfound = false;
//@JA - Then generate the array we will use for the conditions
$conditionarray['conditions'][] = $conditionstring;
foreach($uniques as $unique){
$conditionarray['conditions'][] = $this->read_attribute($unique);
if(array_key_exists($unique, $dirty)){
$dirtyfound = true;
}
}
if ($dirtyfound == true) { //@JA - If one of the parts that makes the record unique is dirty then...
try {
//@JA - Whatever the primary key currently is return the object for that. This will be the object reference for what is not modified
$currently = Self::find($this->id);
}
catch (Exception $e) {
$currently = false;
}
foreach($uniques as $unique){
if ((
(is_object($currently) && $currently->$unique != $this->$unique)
|| !is_object($currently)
) && static::exists($conditionarray))
$this->errors->add($unique, $rules['message']);
}
}
}else{ //@JA - Otherwise use the SINGLE method
$unique = $rules[0];
if (array_key_exists($unique, $dirty)) { //@JA - If the value we are checking to be unique has been modified...
try {
//@JA - Whatever the primary key currently is return the object for that. This will be the object reference for what is not modified
$currently = Self::find($this->id);
}
catch (Exception $e) {
$currently = false;
}
//@JA - The dirty attributes array simply contains fields that have been set by our code.
//@JA - Ergo if we have re-applied the same value to our model, it will be classed as dirty even though it has not changed
//@JA - If $currently was returned as an object type AND its original value does not equal the current dirty value of the property on the model
//@JA - OR If the object returned was not an object (meaning it does not currently exists in the database)...
//@JA - OR it could mean that the table is just empty for the first time... Thus
//@JA - AND if the dirty value of the unique was found to exist then a unique was found.
if ((
(is_object($currently) && $currently->$unique != $this->$unique)
|| !is_object($currently)
) && static::exists(array($unique => $this->$unique)))
$this->errors->add($unique, $rules['message']);
}
}
}
}
要在模型中使用它,只需使用语句“use uniquecheck;”在包含引用trait的php文件之后。例如
require_once('traits/uniquecheck.php');//@JA - Helper to check if values are unique
class Client extends ActiveRecord\Model {
use uniquecheck;
public function validate() {
$this->uniquecheck(array(array('company_id','contactfirstname','contactlastname', 'contactphonenumber', 'contactaddress'),'message' => 'Can\'t have duplicate client.'));
}
}
上面显示了如何检查多个唯一项的示例。这将适用于新记录和编辑记录,因为知道哪些字段脏或不脏是明智的
如果您不使用多重唯一性,它的工作原理就是这样
public function validate() {
$this->uniquecheck(array('username','message' => 'Username already in use'));
}
我复制了他们在PHPActiveRecords文档中使用的格式,所以现在应该完全一样了
希望这对其他人有帮助 在黑暗中拍摄:
static $validates_uniqueness_of = array(
array('username')
);
不,这不起作用,尽管尝试得很好,我认为这是我研究中的一个bug,可能从未被修复过。我找到了一份临时工作,但仍然不确定如何处理多个unique