yii2 BaseActiveRecord的findAll()condtions大于或小于 [英] yii2 BaseActiveRecord findAll() condtions greater or less than

查看:1751
本文介绍了yii2 BaseActiveRecord的findAll()condtions大于或小于的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的国家数据库表(如在的的指南),我测试的开发应用。我有场人口,我想创建国家的公共方法模式返回的特定人群限制的所有国家。即返回人口的所有国家x和y之间。

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.

我试过如下:

// models/Country.php
....

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

}

在CountryController:

In the CountryController:

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

它返回一个空数组I,E 阵列()

现在我需要知道如何设置的findAll 的条件,像SQL条件 ...如果人口与GT = 20000人口&LT;!= 40000000 即如何添加比较的条件与使用数组

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?!

的另一个侧面 - 或者可选 - 的问题,的为什么Country.php打电话时的findAll 如下:

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:

未知方法 - 警予\基地\ UnknownMethodException

调用未知的方法:应用程序\控制器\ CountryController ::的findAll()

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

在换句话说,为什么要叫静态?

In other words why must it called statically?

推荐答案

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

Use debug module to see the generated SQL query.

在你的情况将是:

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

正如你可以看到它是绝对错误的。

As you can see it's definitely wrong.

检查该文件<一个href="http://www.yiiframework.com/doc-2.0/yii-db-baseactiverecord.html#findAll%28%29-detail">findAll(),它不适合这样的条件。 使用找到()代替。

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.

请阅读和的this 部分正式文件,以了解如何其中,的查询部分构成。

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 scope. You can read about it here.

你的另一个问题的答案是:你不应该叫的findAll()在对象范围内,因为它是由框架设计静态方法

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

检查警予\ DB \ BaseActiveRecord

public static function findAll($condition)

这篇关于yii2 BaseActiveRecord的findAll()condtions大于或小于的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆