1

I am trying to list values from a treview in a dropdown list, in Yii. So, i already have a function to concatenate the treeview in path like (example primary\secondary\terciary. I call this function passing the ID from the data and the function select the parents, creating list from the database.

public static function getPath($id) {

        $post=PlanoAssunto::model()->find('id=:id', array(':id'=>$id));

        if ($post->id_pai == null) {
        $row=$post->assunto;
        $data= $row;       
        }
        else {
        $row=$post->assunto;
        $row=PlanoAssunto::getPath($post->id_pai).'/'.$row;
        $data= $row;}

        if (isset($data)) { 
            return $data;     
            }
            else { 
                $data = 'Nulo';
                return $data;  
            }               
}

This work fine in a view, using this:

<?php echo $form->labelEx($model,'id_pai');?>

        <?php   if (isset($_GET['id'])){
                echo  PlanoAssunto::model()->getPath($_GET['id']); }
                else { echo "Novo Assunto Principal";}
        ?>

But now i want to populate a select, in the form, with the path list. And I cant run functions inside CHtml::listData. I am trying with this ih the model, with no success:

public static function getTypeOptions() {   
    return  CHtml::listData(PlanoAssunto::model()->findAll(),'id','getPath($id)');           
}

So, what I am doing wrong? I cant manage a way to put a treeview in path format in the value, associated to his id in database.

I cant figure how I can fill the values in ListData, my HTML returns the IDs, but with any data in value.

 <select name="Areaxplanoassunto[idAssunto]" id="Areaxplanoassunto_idAssunto">
<option value="1"></option>
<option value="2"></option>
<option value="3"></option>
<option value="4"></option>
<option value="5"></option>
<option value="6"></option>
<option value="7"></option>
<option value="9"></option>
<option value="10"></option>
<option value="11"></option>
</select>

Tks. (I am from Brasil, so sorry for the english)

1 Answers1

0

I find a solution.

I have to make another function to put in a array the ID and the PATH from its id. So I call this function in CHtml::listData and all works.

This is the code:

public static function getLista(){
        $models = PlanoAssunto::model()->findAll();  

        foreach($models as $model) {
                $row['id'] = $model->id;
                $row['path'] = PlanoAssunto::getPath($model->id);
                $data[] = $row;
        }       
        if (isset($data)) { 
            return $data;     
            }
            else { 
                $data['id'] = 'Nulo';
                $data['path'] = 'Nulo';
                return $data;  
            }                                             
}

And the gettypeoptions taking the result with CHTML::ListData

public static function getTypeOptions() {

    return  CHtml::listData(PlanoAssunto::model()->getLista(),'id','path');           
}

In the _form view

<?php echo $form->dropDownList($model,'idAssunto', PlanoAssunto::model()->getTypeOptions()); ?>

The result is a dropdown with treeview in path format. Any question, i am avaliable to explain more.