1

我有几个具有关系的模型,我想做的是使用我在 DetailView 中提供的别名搜索字段。看起来像这样

<?php $this->widget('bootstrap.widgets.BootGridView',array(
    'id'=>'operations-grid',
    'dataProvider'=>$model->search(),
    'filter'=>$model,
    'columns'=>array(
        'operationType.name:raw:Operation',
        'creation_date:datetime',
        'modification_date:datetime',
        'ammount_usd:raw:Ammount',
        'currency.short',
        /*

        'client_id',
        'organization_id',
        */
        array(
            'class'=>'bootstrap.widgets.BootButtonColumn',
        ),
    ),
)); ?>

我想要的是能够使用列的别名搜索行,例如currency.short. 这样做的正确方法是什么?试图修改这样的search()方法..但我想我错过了一些东西。

public function search()
{
    // Warning: Please modify the following code to remove attributes that
    // should not be searched.

    $criteria=new CDbCriteria;

    $criteria->compare('creation_date',$this->creation_date,true);
    $criteria->compare('modification_date',$this->modification_date,true);
    $criteria->compare('ammount',$this->ammount,true);
    $criteria->compare('ammount_usd',$this->ammount_usd,true);
    $criteria->compare('currency_id',$this->currency_id);
    $criteria->compare('operation_type',operationType::model()->name);
    $criteria->compare('client_id',$this->client_id);
    $criteria->compare('organization_id',$this->organization_id);
$criteria->compare('comment',$this->comment);
    return new CActiveDataProvider($this, array(
        'criteria'=>$criteria,
    ));
}

谢谢。

4

1 回答 1

3

您必须为该属性创建一个虚拟字段。例如在您的主模型中:

private _currencyshort = null;
public function setCurrencyshort($value) {
    $this->_currencyshort = $value;
}
public function getCurrencyshort() {
    if ($this->_currencyshort === null && $this->currency != null)
    {
        $this->_currencyshort = $this->currency->short
    }
    return $this->_currencyshort;
}
public function search() {
    $criteria=new CDbCriteria;
    $criteria->with = array('currency'); // add more elements to array if you want to search by more relations
    $criteria->compare('currency.short',$this->currencyshort);
    // You can also add this field to your sorting criteria
    // ... etc
}

此外,您还必须将主模型currencyshort的方法添加到它声明的行中,例如:rules()'on'=>'search'

array('currencyshort', 'safe', 'on'=>'search'),

然后columns代替currency.short您可以放入currencyshort,它将与过滤器,排序等一起使用。

于 2012-08-23T10:52:23.733 回答