2

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

How to resolve this issue?

 public static function findAdminByUsername($username)
{
    $adminUser = static::find()->where(['username' => $username,'userType'=>'ADMIN','user_status'=>self::STATUS_ACTIVE])->one();
    if(count($adminUser)>0){
        return $adminUser;
    }else{
        return null;
   }
}
7
  • Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. Commented Feb 14, 2018 at 6:58
  • count() does not support in php 7.2.1. Commented Feb 14, 2018 at 7:01
  • if my function returns object, I need to convert it to array or viceversa Commented Feb 14, 2018 at 7:02
  • Still it's unclear what you're asking, add some relevant code and describe problem Commented Feb 14, 2018 at 7:06
  • where is your code in which you are retrieving the object and then trying to count , add the code relevant to your problem Commented Feb 14, 2018 at 7:21

2 Answers 2

4

The thing is you are checking for count > 1 with using ->one() which looks odd, looking at your code you want to return NULL if there is no record found and if you look into documentation the function one() already returns NULL if there are no records found so you are adding extra code and it could easily be reduced to

public static function findAdminByUsername($username)
{
    return static::find()->where(
        [
            'username' => $username,
            'userType' => 'ADMIN',
            'user_status' => self::STATUS_ACTIVE,
        ]
    )->one();
}
Sign up to request clarification or add additional context in comments.

1 Comment

Appropriate one and very well explained. Thank you!
2

You are using find()......->one() so your query should return just an object .. without iteration capabilities.

if you want check if the find() return a value or not then you could check with isset. find()->one() return null if the query fail.

  public static function findAdminByUsername($username)
  {
      $adminUser = static::find()->where(['username' => $username,'userType'=>'ADMIN','user_status'=>self::STATUS_ACTIVE])->one();
      if( $adminUser !== null ){
          return $adminUser;
      }else{
          return null;
     }
  }

if you don't need others that return the result for find()->..one() you could simply return

return static::find()->
  where(['username' => $username,'userType'=>'ADMIN','user_status'=>self::STATUS_ACTIVE])
     ->one();

2 Comments

Never use isset() on plain variables. Use $adminUser !== null instead. At which point it also becomes obvious that the check is entirely useless in this case, as you just end up returning the same value anyway.
@NikiC the find()-->one() in yii2 return an object when a row is found but return null if nothings is found so isset(..) should be correct ..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.