我目前在用户模型中收到以下错误。
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'
);
如果我删除上面的行,那么程序再次运行就好了。所以我知道它与此有关。
根据文档,这应该是格式。 (http://www.phpactiverecord.org/projects/main/wiki/Validations#validates_uniqueness_of)
从库中引用验证功能如下:
第563行 - https://github.com/jpfuentes2/php-activerecord/blob/master/lib/Validations.php
我在ubuntu0.16.04.4上使用PHP版本7.0.15-0
如果这确实是一个错误,有没有人有任何解决方法?
答案 0 :(得分:0)
public static $ validates_uniqueness_of = array( '用户名' )
答案 1 :(得分:0)
好的,我现在创建了一个完整的解决方案。任何能够改善这一点的人都是受欢迎的。
首先创建一个单独的文件并将其命名为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']);
}
}
}
}
要在您的模型中使用此功能,请使用声明'使用uniquecheck;'在包含参考特性的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文档中使用的格式,因此它现在应该完全相同。
希望这有助于其他人!
答案 2 :(得分:0)
MEMBER