如何在yii模型搜索中按文本值搜索整数类型

时间:2015-05-20 00:57:56

标签: php yii

我有一个名称为" author_id"在Document模型中。我想通过adminadvanced search页面中搜索一些记录。但是,我希望author_id字段可以通过文本值填充。我怎么能这样做?

这是Document型号:

public function rules()
{

    return array(
        array('title, content', 'required'),
        array('author_id', 'numerical', 'integerOnly'=>true),
        array('title, content, author_id', 'safe'),

        array('title, content, author_id', 'safe', 'on'=>'search'),
    );
}


public function relations()
{

    return array(
        'author' => array(self::BELONGS_TO, 'User', 'author_id'),
    );
}

public function attributeLabels()
{
    return array(
        'id' => 'ID',
        'title' => 'Title',
        'content' => 'Content',
        'author_id' => 'Author',
    );
}

public function search()
{
    $criteria=new CDbCriteria;

    $criteria->compare('id',$this->id);
    $criteria->compare('title',$this->pertanyaan,true);
    $criteria->compare('content',$this->penanya_id, true);
    $criteria->compare('author_id',$this->author_id);

    return new CActiveDataProvider($this, array(
        'criteria'=>$criteria,
    ));
}

我认为这是_search.php

    <?php $form=$this->beginWidget('booster.widgets.TbActiveForm',array(
        'action'=>Yii::app()->createUrl($this->route),
        'method'=>'get',
    )); ?>
        <div class="box-body">
            <div class="col-sm-6">
            <?php echo $form->textFieldGroup($model,'title', array('widgetOptions'=>array('htmlOptions'=>array('class'=>'span8')))); ?>

            <?php echo $form->textAreaGroup($model,'content', array('widgetOptions'=>array('htmlOptions'=>array('rows'=>6, 'cols'=>50, 'class'=>'span8')))); ?>

            <?php echo $form->textFieldGroup($model,'author_id',array('widgetOptions'=>array('htmlOptions'=>array('class'=>'span5')))); ?>


            </div>
        </div>
        <div class="box-footer clearfix no-border pull-right">
            <?php $this->widget(
                'bootstrap.widgets.TbButton',
                array(
                    'encodeLabel'=>false,
                    'buttonType' => 'submit',
                    'label' => '<span class="glyphicon glyphicon-search"></span> Search Document!',
                    'context' => 'primary',
                )
            ); ?>
            <?php $this->widget(
                'bootstrap.widgets.TbButton',
                array('buttonType' => 'reset', 'label' => 'Reset')
            ); ?>

       </div>
       <div class="clearfix"></div>
    <?php $this->endWidget(); ?>

谢谢你:)

1 个答案:

答案 0 :(得分:1)

在文档模型中,

rules()

中为搜索条件添加author_name
public function rules()
{
    return array(
        ...,
        array('title, content, author_id,author_name', 'safe', 'on'=>'search'),

    );
}

Search()功能,

public function search()
{
    $criteria=new CDbCriteria;

    $criteria->compare('id',$this->id);
    $criteria->compare('title',$this->pertanyaan,true);
    $criteria->compare('content',$this->penanya_id, true);
    $criteria->compare('author_id',$this->author_id);

    // Add search on author name
    $criteria->with = array('author','author2');

    $criteria->compare('auther_name',$this->author_id);

    $criteria->compare('auther_name2',$this->author_id2);

    return new CActiveDataProvider($this, array(
        'criteria'=>$criteria,
    ));
}