首页 > 解决方案 > count():参数必须是Yii2中实现Countable的数组或对象

问题描述

我在验证用户中使用 xammp v3.2.2我收到此错误:

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

我不知道这个错误取决于 xampp、yii2 或 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;
    }
    }

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

错误的第一部分

错误页面的其余部分:

错误页面的其余部分

我需要做什么?

标签: phpyii2xampp

解决方案


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

但一般来说,如果你使用的是最新版本的 PHP,你应该使用最新版本的 Yii - 使用最新版本的框架的过时版本可能会带来很多问题。尤其是 PHP 7.2 带来了很多 BC 中断,并且在这个版本中有很多东西被破坏了。


推荐阅读