>我在 yii 中有一个findByAttributes
,我想知道您如何输出所有符合指定条件
我在下面举一个例子:
假设在我的Test
表中,我有 3 行test1
属性中的值1
,那么我的视图中有这段代码
$a= Test::model()->findByAttributes(array('test1'=> '1'));
$b= $a-> id;
print_r($b);
我注意到此代码将打印 id "1"而不是"1 2 3"。
我可以使用什么代码来输出所有 test1
属性中带有 1 的 id?
对不起,初学者的问题。我希望任何人都可以提供帮助...
如果要
获取所有记录,则必须使用方法findAllByAttributes(http://www.yiiframework.com/doc/api/1.1/CActiveRecord#findAllByAttributes-detail)而不是findByAttributes。方法 findAllByAttributes 将返回所有满足要求的行,然后您应该迭代它以获取 id。
例
foreach( $models as $model) {
print_r($model->id);
}
但是如果你只想获取id,最好使用CDbCommand和querycolumn。
Yii::app()->db
->createCommand()
->select('id')
->from('Test')
->where('test1=:value', array(':value'=> 1))
->queryColumn()
它将返回一个 id 数组
用于检索满足特定条件的所有行,您可以使用
findAllByAttributes();
为了获得所有结果,您必须循环例如此结果:
$a= Test::model()->findAllByAttributes(array('test1'=> '1'));
foreach($a as $key => $value) {
echo $value->id
}