【问题标题】:count(): Parameter must be an array or an object that implements Countable in Yii2count():参数必须是Yii2中实现Countable的数组或对象
【发布时间】:2018-08-22 10:54:44
【问题描述】:

我在验证用户时使用 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;
    }
    }

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

错误页面的其余部分:

我该怎么办?

【问题讨论】:

  • $targetAttribute 到底是什么?错误说它需要一个数组,但 ist 不是。
  • 显示您应用了唯一验证器的验证
  • @DieterKräutl 我不知道,它在 yii2 上
  • @NitinPund 你能告诉我更多吗?你是说我的示范规则吗?
  • 是的,你的模型规则

标签: php yii2 xampp


【解决方案1】:

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

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

【讨论】:

  • 你确定如果我升级我的 yii2 我的问题会解决吗?
  • 是的,您可以在链接的 PR 中看到该行在 2.0.13 版本中已更改。
  • 是的,它有效,非常感谢。我升级到 Yii 2.0.11 并解决了我的问题。当然第一次升级作曲家更新有问题,但是第二次升级了。
猜你喜欢
  • 2019-04-29
  • 2019-05-10
  • 2021-05-30
  • 2018-06-28
  • 2018-12-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-27
相关资源
最近更新 更多