Question

[SOLVED] I need to compare two ID tables to one Detail table in order to filter my CGridview.

The "Detail" table has has three important fields:

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

There can only be assigned EITHER a PERSON OR a GROUP. BOTH CANNOT be assigned. But at the same time NEITHER of them have to be in (either PERSON, GROUP or NONE)

then I have two ID tables for PERSON and GROUP. basic with only an id linked to detail and a NAME defined as NAME.

in my CGridView I want to add both GROUP and PERSON in one field as SUPPORT because I know that they wont clash because of the EITHER rule earlier explained. so those values I've got in, as in such:

In columns array:

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

easy enough but the difficult part is that I need to add a filter to this column.

so my logic tells me to use $criteria->compare() in my model to compare the two ID tables to the two columns in the DETAIL table. I have used $criteria->compare() to reference a ID table for text field filter, so I have some knowledge on that.

But if there's anyone out there who can manipulate a model well, please share your knowledge because I am lost.

[ADDED SOURCE CODE] Grid View ::

$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
                    )
                ),
            ),
      );

model ::

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

Is it not that you have to be able to search the value for the filter ? or am I under the wrong impression ??

Was it helpful?

Solution

I suggest you to add a calculated field to your Detail model:

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

Now, with Yii's __get function you can access the result of the above method as a property (e.g. $data->assignedSupport), like if it was an attribute from the database. It is a much easier and also cleaner way to add a calculated column to the model, than to the view.

EDIT: To create custom filter and ordering for the calculated column in the gridview, you also have to edit the search() method in the model. Check out the following article for a practical example: Sort and filter a custom or composite CGridView column

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top