Yii$标准->;将两个表与另一个表进行比较';s值


Yii $criteria->compare to compare two tables to another table's values

[已解决]为了过滤我的CGridview,我需要将两个ID表与一个Detail表进行比较。

"详细信息"表有三个重要字段:

1 - id of the detail row
2 - ID of an assigned PERSON
3 - ID of an assigned GROUP (of PERSONs)

只能分配一个人或一个组。两者都不能分配。但同时,他们中的任何一个人都不必在中(个人、团体或无)

那么我有两个PERSON和GROUP的ID表。基本,只有一个id链接到详细信息,NAME定义为NAME。

在我的CGridView中,我想在一个字段中添加GROUP和PERSON作为SUPPORT,因为我知道它们不会因为前面解释的EITHER规则而冲突。所以我得到的那些值,比如:

列阵列中:

array(
    'header'=>'supp group/person',
    'value'=>'(!$data->assignedSupportperson && !$data->assignedSupportgroup ? 
        "null" : 
        ($data->assignedSupportperson ? 
            $data->assignedSupportperson->name : 
            $data->assignedSupportgroup->name
        )
    )',
),

很简单,但困难的部分是我需要向该列添加一个过滤器

所以我的逻辑告诉我在模型中使用$criteria->compare()来将两个ID表与DETAIL表中的两列进行比较。我已经使用$criteria->compare()来引用文本字段过滤器的ID表,所以我对此有一些了解。

但如果有人能很好地操纵模型,请分享你的知识,因为我迷路了。

[添加源代码]网格视图::

$model = new TicketDetail('search');
    if (isset($_GET['TicketDetail'])) {
        $model->attributes = $_GET['TicketDetail'];
    }
    $this->widget('bootstrap.widgets.TbGridView', array(
        'id' => 'Assigned-Ticket-grid',
        'dataProvider'=>$model->assignedToUser(Yii::app()->user->data()->id)->search(),
        'template' => "{items}{pager}",
        'htmlOptions'=>array(),
        'itemsCssClass' => 'table table-striped table-bordered table-condensed',
        'filter' => $model,
        'columns' => array(
            array(
                'name' => 'id',
                'header' => 'ID',
                'headerHtmlOptions'=>array(
                    'width'=> 50,
                ),
            ),
            array(
                    'header'=>'supp group/person',
                    'value'=>'$data->AssignedSupport?$data->AssignedSupport:"null"',
                    //'filter'=>$model,    <----- heres where i tried a couple things.
                    'headerHtmlOptions'=>array(
                        'width'=>100
                    )
                ),
            ),
      );

型号::

public function getAssignedSupport()
    { 
    return !$this->assignedSupportperson&&!$this->assignedSupportgroup?"null":$this->assignedSupportperson?$this->assignedSupportperson->name:$this->assignedSupportgroup->name); 
    }

不是说你必须能够搜索过滤器的值吗?还是我有错误的印象??

我建议您在Detail模型中添加一个计算字段:

public function getAssignedSupport()
{ 
  return !$this->assignedSupportperson && !$this->assignedSupportgroup?"null":($this->assignedSupportperson?$this->assignedSupportperson->name:$this->assignedSupportgroup->name;
}

现在,使用Yii的__get函数,您可以将上述方法的结果作为属性(例如$data->assignedSupport)访问,就像它是数据库中的属性一样。将计算柱添加到模型中比添加到视图中要简单得多,也更干净。

编辑:要为网格视图中的计算列创建自定义筛选器和排序,还必须编辑模型中的search()方法。查看以下文章以获取一个实际示例:对自定义或复合CGridView列进行排序和筛选