Strip down by attributes a large set of models
I have a Yii controller action that can potentially return a very large set of rows that are then encoded into JSON. Since the client that I am building only needs a few columns or properties of the model, I would like to strip down the set to only return certain columns. What I have now is something like:
$models = MyModel::model()->findAll($criteria); $rows = $model->attributes; $this->_sendResponse(200, CJSON::encode($rows));
I feel like the place to do it would be in the second line but I am new to Yii and do not know how to do this. Any help is appreciated!
maybe you could not remove unnecessary columns, but send necessary?
$models = MyModel::model()->findAll($criteria); // do foreach loop with $models ? $dataToSend = array() $dataToSend['id'] = $model->id; $dataToSend['name'] = $model->name; $dataToSend['moreparam'] = $model->moreparam; //send data $this->_sendResponse(200, CJSON::encode( $dataToSend ));
So this code is more readable and we know what are we sending. If you use removing some attributes later maybe you will forget what you are leaving.