2014-04-29 47 views
2

[求助] 我需要比较两个ID表到一个详细信息表,以便过滤我的CGridview。

“详细信息”表中有三个重要领域:

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

只能有可以使用一人或一组。两者不能分配。但在同一时间他们都不得不在(PERSON,GROUP或NONE)

然后我有两个身份证PERSON和GROUP的表。 基本,只有一个链接到详细信息的ID和一个名称定义为NAME。

在我的CGridView中我想在一个字段中同时添加GROUP和PERSON作为支持,因为我知道他们不会因为之前解释过的规则而发生冲突。因此这些值我有,用在这样的:

在列数组:

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); 
    } 

这不是说你必须要能够搜索过滤器的价值?还是我在错误的印象?

回答

0

我建议你到一个计算字段添加到您的Detail模型:现在

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

,与谊的__get功能,您可以访问上述方法的结果为属性(如$data->assignedSupport)一样,如果它是来自数据库的属性。将计算列添加到模型比视图更容易也更简洁。

编辑: 要创建自定义过滤器和排序的GridView中计算列,您还可以编辑在模型中search()方法。看看下面的文章为一个实际的例子: Sort and filter a custom or composite CGridView column

+0

所以,而不是试图通过gridview过滤我给它的错觉,有一个字段的名称值在..好想法..这样会通过幻影场进行过滤,并像来自数据库那样工作......谢谢你,先生,生病了,试试看,并告诉你结果如何。 – Chris

+0

我添加了该功能,它完美地添加了我想要的值。但顶部仍然没有滤镜框。是否有任何额外的东西需要添加到过滤器的gridview标题或不?即时通讯相当新的一切,所以我真的很感谢帮助 – Chris

+0

你可以分享你的网格视图的源代码? – mcserep