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?