Kyl*_*Van 12 php attributes params yii conditional-statements
我一直在使用Yii一段时间,当我想从数据库中提取数据时,我通常只使用findByAttributes.
$model=Auction::model()->findAllByAttributes(array('status'=>'1'));
Run Code Online (Sandbox Code Playgroud)
或类似的规定.
我的问题是,我将如何处理大于类型的情况?我试过了
$model=Auction::model()->findAllByAttributes(array('starttime'>=$date));
Run Code Online (Sandbox Code Playgroud)
其中日期已分配当前日期/时间设置,但这会导致错误.所以我的问题是我需要使用条件和/或参数吗?我应该在模型中使用Criteria或CActiveDataProvider这样的东西吗?
我希望有人指出我正确的方向.我总是通过使用findAll()来获得,但我知道他们是更好的方法.关于什么以及何时使用属性,条件,参数等的一般信息也很好.
我已阅读Yii文档并搜索了大量网站以获取这些问题的答案,我找不到它.
ldg*_*ldg 20
使用params甚至使用findByAttributes是个好主意,但这只是匹配.您可以使用findAll并添加条件语句,其格式与您正在执行的操作非常相似:
$model=Auction::model()->findAll(array(
'condition'=>'status=:status AND starttime >= :date',
'params'=>array(':status'=>1, ':date'=>$date),
));
Run Code Online (Sandbox Code Playgroud)
如果您正在执行非常复杂的查询或以编程方式构建查询,则可能需要使用findAllBySql或CDbConnection :: createCommand或Query Builder,它只取决于对您的应用程序最有意义的内容.
我会(重新)阅读有关使用数据库的Yii部分,虽然它没有大量的例子,但很清楚.然后你可以尝试Yii Blog教程,Larry Ullman的教程等.
第二个参数的类型findAllByAttributes是混合的,因此我们有(不是无限的)许多可能性,例如:
$model = Auction::model()->findAllByAttributes(
array(
'status' => 1
),
'starttime >= :date',
array(
'date'=>$date
)
));
Run Code Online (Sandbox Code Playgroud)
要么
$model = Auction::model()->findAllByAttributes(
array(
'status' => 1
),
array(
'condition' => 'starttime >= :date'
'params' => array('date'=>$date)
)
));
Run Code Online (Sandbox Code Playgroud)