I am currently getting the following error in my User model.
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
My model only crashes the program when I add the line below to it.
static $validates_uniqueness_of = array(
'username'
);
If I remove the line above then the program runs again just fine. So I know it has something to do with this.
According to the documentation this should indeed be the format. ( http://www.phpactiverecord.org/projects/main/wiki/Validations#validates_uniqueness_of )
Reference to the validate function from the library is below:
Line 563 -- https://github.com/jpfuentes2/php-activerecord/blob/master/lib/Validations.php
I'm using PHP Version 7.0.15-0 on ubuntu0.16.04.4
If this is truly a bug, does anyone have any workarounds?
公共静态$ validates_uniqueness_of = array('username')
Ok I created a complete solution now. Anyone who can improve this is welcome.
First create a separate file and call it uniquecheck.php.
Inside it put this trait code.
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']);
}
}
}
}
To use this in your model just use the statement 'use uniquecheck;' after including the php file with reference to the trait. For example...
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.'));
}
}
The above shows an example of how to check a multiple unique. This will work for new records AND for editing records since the trait is smart to know which fields are dirty or not.
If you are not using a multi-unique it works just like this instead.
public function validate() {
$this->uniquecheck(array('username','message' => 'Username already in use'));
}
I copied the format that they use on PHPActiveRecords documentation so it should work exactly the same now.
Hope this helps someone else!
shot in the dark:
static $validates_uniqueness_of = array(
array('username')
);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.