php yii2 BaseActiveRecord findAll() 条件大于或小于

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27894148/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:36:01  来源:igfitidea点击:

yii2 BaseActiveRecord findAll() conditions greater or less than

phpactiverecordyii2

提问by SaidbakR

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

我有测试yii2开发应用程序的国家/地区数据库表(如指南中所示)。我有领域,我想在模型中创建一个公共方法来返回所有特定人口限制的国家。即返回 x 和 y 之间的所有人口国家。populationCountry

I tried the following:

我尝试了以下方法:

// models/Country.php
....

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

}

In the CountryController:

在 CountryController 中:

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

It returns an empty array i,e Array ()

它返回一个空数组 i,e Array ()

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

现在我需要知道如何将条件设置findAll为类似于 SQL 条件,... Where population >= 20000 AND population <= 40000000即如何使用数组添加与条件的比较?!

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

另一个侧面 - 或可选 - 问题,为什么在 Country.php 中调用时findAll如下:

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()

未知方法 – yii\base\UnknownMethodException

调用未知方法:app\controllers\CountryController::findAll()

In other words why must it called statically?

换句话说,为什么它必须静态调用?

回答by arogachev

Use debug module to see the generated SQL query.

使用调试模块查看生成的 SQL 查询。

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.

检查findAll()的文档,它不适合这种情况。使用find()来代替。

1)

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)

2)

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

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

还将方法的声明更改为,static因为它不依赖于对象实例。

Please read thisand thissections of official documentation to understand how wherepart of query is constructed.

请仔细阅读这个正式文件的部分,以了解如何where查询的一部分构造。

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.

您的其他问题的答案:您不应该findAll()在对象上下文中调用,因为它是框架设计的静态方法。

Check the yii\db\BaseActiveRecord:

检查yii\db\BaseActiveRecord

public static function findAll($condition)