2012-12-14 37 views
0

我有如下表:Yii中顯示多個子元素作爲鏈接CGridView場

CREATE TABLE "place" (
    "id" int8 NOT NULL DEFAULT nextval('place_id_seq'::regclass), 
    "name" varchar(128) NOT NULL, 
    "parent" int8, 
    "description" varchar(100) 
) 
WITH (OIDS=FALSE); 

(這是PostgreSQL的,但這不應該在這裏有關)

我通過giix創建的CRUD和改變了一點關係,以配合我的需求。所以我結束了:

public function relations() { 
    return array(
     'parent0' => array(self::BELONGS_TO, 'Places', 'parent'), 
     'places' => array(self::HAS_MANY, 'Places', 'parent'), 
    ); 
} 

這裏的目標是,一個地方可以屬於另一個地方,並有多個兒童的地方。

我的問題是,我需要改變管理措施,以配合下面的網格: Print manipulated by the inspector to reflect the desired behaviour

打印檢查員操縱,以反映預期的行爲

所以我的問題是讓列表所有的孩子都會反對並將其顯示爲一個鏈接列表。我試着更改相應的admin.php的視圖文件:

$this->widget('zii.widgets.grid.CGridView', array(
    'id' => 'places-grid', 
    'dataProvider' => new CActiveDataProvider('Places', array('id'=>$model->places)), 
    'filter' => $model, 
    'columns' => array(
     array(
       'name'=>'parent', 
       'value'=>'GxHtml::valueEx($data->parent0)', 
       'filter'=>GxHtml::listDataEx(Places::model()->findAllAttributes(null, true)), 
       ), 
     'name', 
     'places', 
     array(
      'class' => 'CButtonColumn', 
     ), 
    ), 
)); 

但這一如預期,得來的錯誤:

htmlspecialchars() expects parameter 1 to be string, array given

此外,我不知道這甚至會與內置的工作高級搜索。

任何人都可以在這裏指出我正確的方向嗎?

+0

您所有的疑問已經在這裏對S.O解決方案,處理它們一次一個,首先要過濾顯示正確,然後再爲使搜索工作,然後鏈接。使用s.o.搜索,你會找到答案。 –

回答

1

經過大量的搜索,我找到了解決問題的方法。不知道這是最好的方式,但它工作得很好:

$this->widget('zii.widgets.grid.CGridView', array(
    'id' => 'places-grid', 
    'dataProvider' => new CActiveDataProvider('Places', array('id'=>$model->places)), 
    'filter' => $model, 
    'columns' => array(
     array(
       'name'=>'parent', 
       'value'=>'GxHtml::valueEx($data->parent0)', 
       'filter'=>GxHtml::listDataEx(Places::model()->findAllAttributes(null, true)), 
       ), 
     'name', 
     array(
     'header'=>'Children', 
     'type'=>'html', 
       'value'=> function($data) { 
        $placesLinks = array(); 
        foreach ($data->places as $place) { 
         $placesLinks[] = GxHtml::link(GxHtml::encode(GxHtml::valueEx($place, 'name')), array('places/view', 'id' => GxActiveRecord::extractPkValue($place, true))); 
        } 
        return implode(', ', $placesLinks); 
       } 
     ), 
     array(
      'class' => 'CButtonColumn', 
     ), 
    ), 
)); 

這使用Giix。如果你不使用它,那麼你將不得不相應地更改鏈接生成。

乾杯

+0

很好,你有你自己的解決方案工作,對於搜索請參閱[這裏](http://stackoverflow.com/a/4865411/720508) –