count(): 参数必须是数组或者是Yii2中实现Countable的对象

count(): Parameter must be an array or an object that implements Countable in Yii2

我在验证用户时使用 xammp v3.2.2 我遇到了这个错误:

count(): Parameter must be an array or an object that implements Countable

不知道这个错误依赖于xampp, yii2 or php v7.2.3.

in C:\xampp\htdocs\payesh\vendor\yiisoft\yii2\validators\UniqueValidator.php at line 136

        if ($this->modelExists($targetClass, $conditions, $model)) {
            if (count($targetAttribute) > 1) { //error mentions here
                $this->addComboNotUniqueError($model, $attribute);
            } else {
                $this->addError($model, $attribute, $this->message);
            }
        }
    }

这是我的用户模型:

public function signup() {
     if ($this->validate()) { // the error mentions here too
            $user = new User();
            $user->username = $this->email;
            $user->email = $this->email;
     if ($user->save(false)) {
                return $user;
    }
    }

这张错误页面图片可能有助于更好地理解:

错误页面的其余部分:

我需要做什么?

您需要升级到 Yii2 的最新版本(或至少 2.0.13)- 与 PHP 7.2 的兼容性在 2.0 13 版本中得到了显着改进,请参阅 this PR

但一般来说,如果您使用的是 PHP 的最新版本,您应该使用 Yii 的最新版本 - 使用最新版本的框架和最新的 PHP 版本可能会带来很多问题。特别是 PHP 7.2 带来了很多 BC 漏洞,很多东西在这个版本中被破坏了。