tenho uma tabela Cliente com vários campos mas gostaria de exibir apenas nome, cpf e telefone para uma determinada requisição.
Abaixo segue o meu Model:
class Cliente extends CActiveRecord {
/**
* @return string the associated database table name
*/
public function tableName() {
return 'cliente';
}
/**
* @return array validation rules for model attributes.
*/
public function rules() {
return array(
array('nome, cpf, fone', 'safe', 'on' => 'search'),
);
}
/**
* Retrieves a list of models based on the current search/filter conditions.
*
* Typical usecase:
* - Initialize the model fields with values from filter form.
* - Execute this method to get CActiveDataProvider instance which will filter
* models according to data in model fields.
* - Pass data provider to CGridView, CListView or any similar widget.
*
* @return CActiveDataProvider the data provider that can return the models
* based on the search/filter conditions.
*/
public function search() {
$criteria = new CDbCriteria;
$criteria->compare('nome', $this->nome, true);
$criteria->compare('fone', $this->fone, true);
$criteria->compare('cpf', $this->cpf, true);
$criteria->select = 'nome, cpf, fone';
return new CActiveDataProvider($this, array(
'criteria' => $criteria,
));
}
/**
* Returns the static model of the specified AR class.
* Please note that you should have this exact method in all your CActiveRecord descendants!
* @param string $className active record class name.
* @return Cliente the static model class
*/
public static function model($className = __CLASS__) {
return parent::model($className);
}
}
Na sua view correspondente a model Cliente, faça algo como abaixo, considerando que os campos nome, cpf e fone no formulário serão exibidos ao receber a requisição via POST com um determinado parâmetro que eu chamo no exemplo de "seu_parametro":