我有一个名称为" author_id
"在Document
模型中。我想通过admin
在advanced 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(); ?>
谢谢你:)
答案 0 :(得分:1)
在文档模型中,
在rules()
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,
));
}