Search code examples
yiiyii-extensionsgii

Showing a specific attribute when generating CRUD in Yii web framwork using giix extenstion


I am generating Models and CRUD for my database tables using giix in YII web framework, the thing is I want to change some of the attributes that showed to me but I dont know how ? I get into the code _FORM.php of the generated CRUD to one of the table and I knew the piece of code that I must change it to get a different attribute instead of one that shown to me without knowing why ?

    <div class="row">
    <?php echo $form->labelEx($model,'idEmployee'); ?>
    <?php echo $form->dropDownList($model, 'idEmployee', GxHtml::listDataEx(Employee::model()->findAllAttributes(null, true))); ?>
    <?php echo $form->error($model,'idEmployee'); ?>
    </div><!-- row -->

in the previous code the form showed a drop-down list from another table jointed with the current table according to idEmployee, he's showing an attribute that I dont want, I want to know how to render the FirstName and the LastName in the drop-down list, any help please ?


Solution

  • I believe it is easier when you just create your own dropdown list provider

    in the Employee.php you add these two functions:

    public function getFullName()
    {
        return $this->first_name.' '.$this->last_name; // or what ever you want to be shown on the drop list
    }
    
    
    public static function getNamesList() {
        return CHtml::listData(self::model()->findAll(), 'idEmployee', 'fullName');
    }
    

    in the _FORM.php write:

    <div class="row">
    <?php echo $form->labelEx($model,'idEmployee'); ?>
    <?php echo $form->dropDownList($model, 'idEmployee', Employee::getNamesList()); ?>
    <?php echo $form->error($model,'idEmployee'); ?>
    </div><!-- row -->