Giix Dropdownlist + Nestedsetbehavior + Virtual Attributes +  's

Hi Guys,

I don’t know if anybody has faced such a situation, but I can’t really find relevant info on the net.

I’m using Giix and nestedsetbehavior.

based on this Wiki: Understanding Virtual Attributes and get/set methods

I made a virtual attribute:




public function getNbspName() {

    return str_repeat(html_entity_decode(" "), $this->level*4) . $this->name;

}



this way, in the view, it looks as I want.

The problem: I wanted to echo the variable NbspName in the related table’s create form, to kinda have the same tabbed design in the dropdown:




<?php echo $form->dropDownList($model, 'foreign_id',

GxHtml::listDataEx(Model::model()->findAllAttributes(null, true), 'id', 'NbspName')); ?>



why is it not rendered in the dropdown list the same way, with visible nbsps? If I put nbsps manually into a select list, it is rendered, so the problem is somewhere else.

And the order of data is also not correct.

Or should I try a different approach?

Thanks!

c

Hi,

I don’t think that this is the definitive solution for your problem, but I believe that you should name the method “getNbspName” instead.

Hey Guys,

I’ve figured it out! I was just monkeying around and experimenting, at least to find a solution for the sorting at first.

I don’t really understand why, but changing to this:




<?php echo $form->dropDownList($model, 'foreign_id', GxHtml::listDataEx(

Model::model()->findAll(array('order' => 'lft ASC')), 'id', 'NbspName')); ?>



suddenly solved BOTH problems. Now the dropdown looks exactly the way I wanted!

Hi Guys,

a new problem I’ve found in connection with my virtual attribute, on admin page I can’t search in the gridtop search on NbspName, but I can in the advanced search.

What can be the problem?

I see: because in the advanced search, I search for the original name, and down at the gridtop search, it wants to search directly to NbspName, because the view looks like this:




$this->widget('zii.widgets.grid.CGridView', array(

    'id' => 'model-grid',

    'dataProvider' => $model->search(),

    'filter' => $model,

    'columns' => array(

        'id',

        'NbspName',

        ...



however NbspName doesn’t really exist this way. Is there a way to make the gridtop search believe to search for a different value than what is shown in the columns section?

I tried to do this (looks straightforward to me):




public function search() {

...

$criteria->compare('name', $this->NbspName, true);

...

}



but no luck…

Thanks!

UPDATE: this way it’s good.

however, for now, I think every data is to use it, one would like to search for it, to handle it, etc so, I don’t really see the point of virtual attributes, but I’m just a beginner so maybe later I will find the real use of it.

ahh, still not completely good. I thought adding this:




$criteria->select = array("*", "CONCAT(REPEAT('&nbsp;', level), name) AS nbsp_name");



will solve the problem, but now all nbsps are printed as ‘&nbsp;’. how can I add the php function html_entity_decode to this custom column?

UPDATE: I have found another solution:




$this->widget('zii.widgets.grid.CGridView', array(

    'id' => 'uzem-grid',

    'dataProvider' => $model->search(),

    'filter' => $model,

    'columns' => array(

        'id',

        array(

            'name' => 'nbsp_name',

            'type' => 'raw',

        ),

        ...



this way, the nbsps are rendered normally.

and for the related table’s create _form I still need a magic GET function also, otherwise the dropdown is not working. :blink:

BEWARE: the variable name and the function name (without the ‘get’ part) must not be the same!

but now at last everything is working.

Thanks

c