在yii 1.1中输出所有'findbyattributes'

时间:2016-03-13 14:47:40

标签: php yii find

我在yii中有findByAttributes,我想知道如何输出符合指定条件的所有数据

我有一个例子如下:

假设在我的Test表中我有3行,1属性中的值为test1,那么我在我的视图中有这个代码

$a= Test::model()->findByAttributes(array('test1'=> '1'));
$b= $a-> id;
print_r($b);

我注意到这段代码会打印id为'1'而不是'1 2 3'。

我可以使用哪些代码,以便在test1属性中输出1的所有ID?

对不起初学者的问题。我希望任何人都可以提供帮助...

2 个答案:

答案 0 :(得分:1)

如果要获取所有记录,则必须使用方法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

答案 1 :(得分:1)

对于retrive所有行,符合您可以使用的特定条件

 findAllByAttributes();

为了获得所有结果,你必须循环,例如,这个结果:

$a= Test::model()->findAllByAttributes(array('test1'=> '1'));

foreach($a as $key => $value)  {

 echo $value->id
}