首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHPActiveRecord validates_uniqueness_of不工作

PHPActiveRecord validates_uniqueness_of不工作
EN

Stack Overflow用户
提问于 2017-04-30 09:40:03
回答 3查看 352关注 0票数 0

目前,我的用户模型中出现了以下错误。

代码语言:javascript
复制
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

我的模型只有在我将下面一行添加到它时才会崩溃。

代码语言:javascript
复制
static $validates_uniqueness_of = array(
      'username'
 );

如果我删除上面的行,那么程序就会再次正常运行。所以我知道这和这事有关。

根据文件,这确实应该是格式。(of)

对库中的验证函数的引用如下:

第563行-- https://github.com/jpfuentes2/php-activerecord/blob/master/lib/Validations.php

我在ubuntu0.16.04.4上使用PHP7.0.15-0版本

如果这是一个真正的错误,有人有什么解决办法吗?

EN

回答 3

Stack Overflow用户

发布于 2017-04-30 10:18:28

好了,我现在创建了一个完整的解决方案。任何能改善这一点的人都是受欢迎的。

首先创建一个单独的文件,并将其命名为uniquecheck.php。

在里面放上这个特征代码。

代码语言:javascript
复制
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文件并引用该特性之后。例如..。

代码语言:javascript
复制
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.'));
     }

}

上面展示了如何检查多个唯一的示例。这将适用于新记录和编辑记录,因为该特性明智地知道哪些字段是脏的还是不脏的。

如果您没有使用多唯一,它的工作方式就像这样。

代码语言:javascript
复制
public function validate() {
    $this->uniquecheck(array('username','message' => 'Username already in use'));
}

我复制了他们在PHPActiveRecords文档中使用的格式,所以现在应该完全一样。

希望这能帮到别人!

票数 0
EN

Stack Overflow用户

发布于 2017-04-30 09:44:33

公共静态$validates_uniqueness_of =数组(“用户名”)

票数 -1
EN

Stack Overflow用户

发布于 2017-04-30 10:21:29

在黑暗中开枪:

代码语言:javascript
复制
static $validates_uniqueness_of = array(
      array('username')
 );
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43705005

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档