2012-12-14 38 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) –