Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

yii2 BaseActiveRecord findAll() conditions greater or less than

I have country database table (like found in the guide) that I test yii2 development application. I have field population and I want to create a public method in Country model to return all countries of specific population limits. i.e return all countries of population between x and y.

I tried the following:

// models/Country.php
....

public function getPopulationBetween($lower, $upper)
{
  return Country::findAll(['population' => [">=".$lower, "<=".$upper]]);

}

In the CountryController:

public function actionGetBetween($lower, $upper)
    {
      print_r(Country::getPopulationBetween($lower, $upper));
    }

It returns an empty array i,e Array ()

Now I need to know how to set the condition of findAll to be like the SQL condition ... Where population >= 20000 AND population <= 40000000 i.e How to add comparison to the condition with using an array?!

Another side -or optional- question, Why in Country.php when calling findAll as follows:

public function getPopulationBetween($lower, $upper)
    {
      return $this->findAll(['population' => [">=".$lower, "<=".$upper]]);

    }

It returns an error:

Unknown Method – yii\base\UnknownMethodException

Calling unknown method: app\controllers\CountryController::findAll()

In other words why must it called statically?

like image 785
SaidbakR Avatar asked Jan 12 '15 01:01

SaidbakR


People also ask

What is ActiveRecord in Yii2?

Advertisements. Active Record provides an object-oriented API for accessing data. An Active Record class is associated with a database table. Yii provides the Active Record support for the following relational databases −

Is null in Yii2 query?

Yii2 will use "IS NULL" if the $values === null , but in case the value is supplied as an array, and one of those array elements is null, it will not get any special treatment, resulting in the query never matching any records with NULL value.


1 Answers

Use debug module to see the generated SQL query.

In your case it will be:

SELECT * FROM `countries` WHERE `population` IN ('>=20000', '<=40000000')

As you can see it's definitely wrong.

Check the documentation for findAll(), it's not suitable for such condition. Use find() instead.

1)

public static function getPopulationBetween($lower, $upper)
{
    return Country::find()
        ->where(['and', "population>=$lower", "id<=$upper"])
        ->all();
}

Note that in this case quoting and escaping won't be applied.

2)

public static function getPopulationBetween($lower, $upper)
{
    return Country::find()
        ->where(['>=', 'population', $lower])
        ->andWhere(['<=', 'population', $upper])
        ->all();
}

Also change declaration of the method to static since it's not dependent on object instance.

Please read this and this sections of official documentation to understand how where part of query is constructed.

Maybe it's better to put this method in customized query class. You can read about it here.

The answer for your additional question: you should not call findAll() in object context because it's static method by framework design.

Check the yii\db\BaseActiveRecord:

public static function findAll($condition)
like image 164
arogachev Avatar answered Oct 02 '22 15:10

arogachev