php Yii findByAttributes() 大于属性。
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7314284/
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
Yii findByAttributes() with greater than attribute.
提问by KyleVan
I have been using Yii for a while now and when I want to pull data from a database I generally just use a findByAttributes.
我已经使用 Yii 一段时间了,当我想从数据库中提取数据时,我通常只使用 findByAttributes。
$model=Auction::model()->findAllByAttributes(array('status'=>'1'));
Or something along those lines.
或类似的规定。
My question would be, how would I handle a greater than type situation? I tried
我的问题是,我将如何处理大于类型的情况?我试过
$model=Auction::model()->findAllByAttributes(array('starttime'>=$date));
where date had been assigned a current date/time setup however this causes an error. So my question is do I need to use conditions and or params? Should I do this type of thing in the model and or using the Criteria or CActiveDataProvider stuff?
其中 date 已分配了当前日期/时间设置,但这会导致错误。所以我的问题是我需要使用条件和或参数吗?我应该在模型中做这种类型的事情还是使用 Criteria 或 CActiveDataProvider 的东西?
I would appreciate someone pointing me in the right direction. I have always just gotten by using findAll()s but I know their is a better way to do this. Some general info on what and when to use Attributes, Conditions, Params, etc would also be nice.
我会很感激有人指出我正确的方向。我一直只是通过使用 findAll()s 获得,但我知道他们是一个更好的方法来做到这一点。关于什么以及何时使用属性、条件、参数等的一些一般信息也会很好。
I have read the Yii documentation and searched tons of sites for the answers to these questions and Im not finding it.
我已经阅读了 Yii 文档并搜索了大量网站以寻找这些问题的答案,但我没有找到。
回答by ldg
It's a good idea to use params even with findByAttributes, but that only does matching. You can use findAll and add a condition statement and the format would be very similar to what you are already doing:
即使与 findByAttributes 一起使用 params 也是一个好主意,但这只适用于匹配。您可以使用 findAll 并添加条件语句,格式将与您已经在做的非常相似:
$model=Auction::model()->findAll(array(
'condition'=>'status=:status AND starttime >= :date',
'params'=>array(':status'=>1, ':date'=>$date),
));
If you were doing a very complex query or building a query programmatically you might want to use findAllBySql or CDbConnection::createCommand or Query Builder, it just depends on what makes the most sense for your app.
如果您正在执行非常复杂的查询或以编程方式构建查询,您可能想要使用 findAllBySql 或 CDbConnection::createCommand 或Query Builder,这仅取决于对您的应用程序最有意义的内容。
I would (re)read the Yii section on Working with Databases, while it doesn't have extensive examples, it's pretty clear. Then you can try the Yii Blog tutorial, Larry Ullman's tutorials, etc.
我会(重新)阅读有关使用数据库的 Yii 部分,虽然它没有大量示例,但很清楚。那你可以试试 Yii 博客教程,Larry Ullman 的教程等等。
回答by Radu Dumbr?veanu
The type of the 2nd parameter of findAllByAttributes
is mixed, thus we have (not infinitely but) many possibilities, for example:
的第二个参数的类型findAllByAttributes
是混合的,因此我们有(不是无限的)很多可能性,例如:
$model = Auction::model()->findAllByAttributes(
array(
'status' => 1
),
'starttime >= :date',
array(
'date'=>$date
)
));
or
或者
$model = Auction::model()->findAllByAttributes(
array(
'status' => 1
),
array(
'condition' => 'starttime >= :date'
'params' => array('date'=>$date)
)
));