Yii Framework Forum: Selecionar Colunas Específicas Em Um Find - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Selecionar Colunas Específicas Em Um Find Rate Topic: -----

#1 User is offline   Gilberto Arthur 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 06-August 14

Posted 06 August 2014 - 01:15 PM

Olá,

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);
    }

}

0

#2 User is offline   Davi Crystal 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 17
  • Joined: 16-March 11

Posted 07 August 2014 - 07:01 AM

View PostGilberto Arthur, on 06 August 2014 - 01:15 PM, said:

Olá,

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);
    }

}



Olá Gilberto

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":



<?php if(isset($_POST['seu_parametro'])){ ?>

	<div class="row">
	<?php echo $form->labelEx($model,'nome'); ?>
	<?php echo $form->error($model,'nome'); ?>
	<?php echo $form->textField($model,'nome',array('size'=>60,'maxlength'=>255)); ?>		
	</div>

<div class="row">
	<?php echo $form->labelEx($model,'cpf'); ?>
	<?php echo $form->error($model,'cpf'); ?>
	<?php echo $form->textField($model,'cpf',array('size'=>60,'maxlength'=>255)); ?>		
	</div>

<div class="row">
	<?php echo $form->labelEx($model,'fone'); ?>
	<?php echo $form->error($model,'fone'); ?>
	<?php echo $form->textField($model,'fone',array('size'=>60,'maxlength'=>255)); ?>		
	</div>


<?php } ?>


Davi Crystal #dup
0

#3 User is offline   bmsrox 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 30
  • Joined: 03-August 11

Posted 15 August 2014 - 01:36 PM

De uma olhada nessa doc. Tem explicando como usar o AR.

Usando FIND

para selecionar colunas especificas usando FIND

$post=Post::model()->find(array(
    'select'=>'title',
    'condition'=>'postID=:postID',
    'params'=>array(':postID'=>10),
));

0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users