I am new in yii. I have city name and value in tbl_city and I want to show city comb box on add user page so that user can choose city and submit. After this i have to save city value in database and on edit user page i have to show city combo and select the city value by default on the basis of city id which is store in table. same i have to show on search section and view section.
CHtml::listData() returns an array of value-label pairs. It serves quite well when used in a CActiveForm::dropDownList to generate a list of options.
And when you use CActiveForm::dropDownList, the initial selection of the dropDownList is automatically set according to your model … It should show ‘Select your city’ when you are creating a new model instance, and should show the name of the selected city when you are updating the existing one.
Well, giix should do some kind of magic and automatically generate a form code that is almost similar to the example above … I’m not very sure because I’m not familiar with it. You may try it, it’s one of the most favored extensions.
But I think it’s not too bad to do it manually, especially when you are not accustomed to the concept of relational active record.
That should replace the cityId field in your GridView with the name of the city, including the ability to search and sort on the city name instead of the id.
If you have tables with many:many relations, for example an Image table that you would want to link to User and to City, you will also need to set up a city_grid in your CityImage model, and then in the DetailView of the regular view file, replace the cityId in the attributes array with: