yii2 BaseActiveRecord findAll()条件大于或小于 [英] yii2 BaseActiveRecord findAll() conditions greater or less than

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

问题描述

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



我尝试了以下操作:

  // models / Country.php 
....

公共函数getPopulationBetween($ lower,$ upper)
{
return国家: :findAll(['population'=> [> =。$ lower,< =。$ upper]]));

}

在CountryController中:

 公共函数actionGetBetween($ lower,$ upper)
{
print_r(Country :: getPopulationBetween($ lower,$ upper));
}

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



现在我需要知道如何设置 findAll 的条件为像SQL条件 ...那里的人口> = 20000 AND人口< = 40000000 即如何使用数组向条件添加比较?!



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

 公共函数getPopulationBetween($ lower,$ upper)
{
return $ this-> findAll (['population'=> [> =。$ lower,< =。$ upper]]);

}

返回错误:


未知方法– yii\base\UnknownMethodException



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


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

解决方案

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



在您的情况下,它将是:

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

如您所见,这肯定是错误的。



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



1)

 公共静态函数getPopulationBetween($ lower,$ upper)
{
return Country :: find()
-> where(['and', population> = $ lower, id< = $ upper])
-> all();
}

请注意,在这种情况下,将不使用引号和转义。 p>

2)

 公共静态函数getPopulationBetween ($ lower,$ upper)
{
return Country :: find()
-> where(['> =','population',$ lower])
-> andWhere([[< =','人口',$ upper])
-> all();
}

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



请阅读部分了解如何构建查询的地方部分。



也许最好将此方法放在自定义查询类中。您可以在这里



您的其他问题的答案:您不应在对象上下文中调用 findAll(),因为它是静态的



检查 yii\db\BaseActiveRecord

 公共静态函数findAll($ condition)


I have country database table (like found in the guide) that I test 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?

解决方案

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)

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

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