Php 如何在yii模型搜索中按文本值搜索整型
我在Php 如何在yii模型搜索中按文本值搜索整型,php,yii,Php,Yii,我在文档模型中有一个名为“作者id”的字段。我想通过高级搜索在管理页面中搜索一些记录。但是,我希望author\u id字段可以由文本值填充。我该怎么做 这是文档模型: public function rules() { return array( array('title, content', 'required'), array('author_id', 'numerical', 'integerOnly'=>true), ar
文档
模型中有一个名为“作者id
”的字段。我想通过高级搜索
在管理
页面中搜索一些记录。但是,我希望author\u id
字段可以由文本值填充。我该怎么做
这是文档
模型:
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,
));
}
在我看来,这是\u 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(); ?>
在文档模型中
在rules()
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,
));
}
你需要按作者姓名搜索吗?@YatinMistry:是的。我想按作者的名字搜索。你的回答对我很有帮助。它起作用了!非常感谢:)这对我的关系起了作用。两个或多个关系使用相同的参考表如何?例如,该文档有两位作者。然后我放入:$criteria->with=array('author2')$标准->比较('auther\u name',$this->author2\u id)代码>但它不起作用
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,
));
}