How to iterate over Yii CActiveDataProvider object?

后端 未结 4 1152
礼貌的吻别
礼貌的吻别 2020-12-28 10:17

How to iterate over a dataprovider object? I want to access the \'name\' field of each row returned and build a list. Can you help?

Table structure for table

4条回答
  •  既然无缘
    2020-12-28 10:43

    Try this:

    public function returnCategoryNames()
    {
      $dataProvider= new CActiveDataProvider('Categories');
      $dataProvider->setPagination(false);
      //$count = $dataProvider->totalItemCount();
      $names = array();
      foreach($dataProvider->getData() as $record) {
        $names[] = $record->name;
      }
      return array_unique($names);
    }
    

    However you dont need to use a data provider, instead just use the model

    foreach(Categories::model()->findAll() as $record) {
    

提交回复
热议问题