Dropdown list from database/model

Hi

I searched through cookbook and other sources, but I couldn’t find simple example with good explanation, where to put what…

I have 2 models: A and B. On the view of model A (Business) I am using dictiodary data from model B (DictBusinessTypes).

In Business model (A) I put code to retrieve data from model B (DictBusinessTypes):




public function getBusinessTypes()

    {

        return array(

           CHtml::listData(DictBusinessTypes::model()->findAll(), 'id', 'name_pl'),

        );

    }



Then in the view I display it:




	<div class="row">

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

		<?php echo CHtml::activeDropDownList($model,'id_business_type', $model->getBusinessTypes()); ?>

		<?php echo $form->error($model,'id_business_type'); ?>

	</div>




How come zero is first on the list and I cannot even choose it?

This is the html source:




<optgroup label="0">

<option value="1000001">Banking</option>

<option value="1000002">Accountancy</option>

<option value="1000003">Real Estate</option>

<option value="1000004">Engineering</option>


<option value="1000005">Education</option>

<option value="1000006">Leasing</option>

<option value="1000007">Insurance</option>

<option value="1000008">Brocker  Houses</option>

<option value="1000009">Shared Service Centers</option>

<option value="1000010">Marketing</option>

<option value="1000011">Sales</option>

<option value="1000012">Farmacy</option>

<option value="1000013">IT</option>


<option value="1000014">Legal</option>

<option value="1000015">Audit</option>

<option value="1000016">Construction</option>

<option value="1000017">Energy</option>

<option value="1000018">Manufacturing</option>

<option value="1000019">Logistics</option>

</optgroup>




Could you help me how to get rid of this zero, or write dropdown list code in better way?

Thanks in advance.




  return CHtml::listData(DictBusinessTypes::model()->findAll(), 'id', 'name_pl');



(But I think I would keep all CHtml method calls in the view.)

/Tommy

Thanks Tommy it works!

Zero is no longer on dropdown list. I assume this was because of returning array of listData not just listData in the method in model A.

So how would it look like to put it all in the view? Wouldn’t it be little against pure MVC pattern to interact with another model in view? Now in model A I get data from model B within method


public function getBusinessTypes()

then in the view I call this method to fill the dropdown list (view part of MVC)…

And how to add empty value to the list?

Thanks

Tom

public function getBusinessTypes()

{


    return array(


       CHtml::listData(DictBusinessTypes::model()-&gt;findAll(), 'id', 'name_pl'),


    );


}

function Return A group of Array though Display 0 There In combo box

So u have to Do Just Renove " Return Array Form

public function getBusinessTypes()

{

}