2014-04-29 48 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