3

你好,我对 Yii 框架很陌生,我正在关注Larry Ullman 的教程系列。我有一个员工模型和一个部门模型。Department Model与Employee Modelhas_many关系,departmentId是 Employee Model 中的外键。

管理视图中,我有一个搜索栏,后跟一个员工列表,我想显示部门的名称而不是部门 ID,并通过部门名称进行搜索。为了尝试,我编写了以下代码,其中包含一个对应于 departmentId 字段的数组。这个适用于视图操作,但不适用于管理操作

请帮助。

<?php 
echo CHtml::link('Advanced Search','#',array('class'=>'search-button')); ?>
<div class="search-form" style="display:none">
<?php $this->renderPartial('_search',array(
'model'=>$model,
)); 
?>
</div><!-- search-form -->

<?php 
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'employee-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
    'id',
    array(
    'name'=>'departmentId',
    'value'=>$model->department->name,
    ),
    //'departmentId',
    'firstName',
    'lastName',
    'email',
    'ext',
    /*
    'hireDate',
    'leaveDate',
    */
    array(
        'class'=>'CButtonColumn',
    ),
),
)); ?>
4

2 回答 2

3

我假设您的视图操作只是从单个模型生成的列表视图。此代码在这种情况下不起作用,因为 CGridView 使用从您指定使用的 CActiveDataProvider 返回的数据生成每一行$model->search()。因此$model,在这种情况下,它只是当前模型,不包含您的查询生成的数据。

为了让它工作,value应该是一个 CGridView 可以评估为 PHP 代码的字符串。所以它应该看起来像这样'value'=>'$data->department->name;',$data是 Yii 用来将当前行提供给 CDataColumn 的变量)。

于 2013-10-15T14:21:58.813 回答
1

我发现在 gridview 中创建额外的可搜索关系列的最佳方法是使用以下模式:

// the model class
class Product extends CActiveRecord {
    // create a custom field in your model class to hold your search data
    public $searchSupplierName;

    // [...]

    // make sure the custom field is safe to set in your rules
    public function rules() {
        return array(
            // [...]

            // add your custom field name to this rule
            array('id, searchSupplierName', 'safe', 'on'=>'search'), 
        );
    }

    // edit your search function 
    public function search() {
        // [...]

        // use the value of the custom attribute as a condition in your search
        $criteria->compare('supplier.name', $this->searchSupplierName,true);

        // you could use something like the following 
        // to make sure you don't fire a lazy loading query 
        // for every result that is shown on the gridview.
        if($this->searchSupplierName)
            $criteria->with = array( 'supplier' );

        // then do something like this if you want to be able to sort on the field
        return new CActiveDataProvider($this, array(
            'criteria'=>$criteria,
            'sort'=>array(
                'attributes'=>array(
                    'searchSupplierName'=>array(
                        'asc'=>'supplier.name',
                        'desc'=>'supplier.name DESC',
                    ),
                    '*', // this makes sure the rest of the field sorts keep working
                ),
            ),
        ));
    }
}
// in the template
<?php 
$this->widget('zii.widgets.grid.CGridView', array(
    // set the dataprovider to the changed search function output
    'dataProvider' => $model->search(),
    'filter' => $model, // model is an instance of Product in this case (obviously ;)

    // [...]

    'columns' => array(
        // [...]

        // this is the column we care (extra) about
        // notice I still use a custom value here (as @ethan describes in his answer)
        array(
            'name' => 'searchSupplierName',
            'value' => '$data->supplier->name',
        ),
    ),
)); 
?>  

这个解决方案应该适用于(几乎)任何你想要的情况。如果您想对列进行基于文本的过滤,这是我能想到的最佳解决方案。

但也有其他选择。例如,如果您只想relation_id通过使用组合框在 a 上搜索以找到正确的值,则有不太复杂的解决方案

于 2013-10-18T10:20:10.043 回答