Working with checkboxes, radios and select boxes

Hi all,

I am new to Yii and I need some guidance regarding the general implementation methodology for few of the issues/tasks that I have.

In my application, almost all the forms have check boxes/radios/select boxes, whose options are actually stored in database tables. And while displaying the form, I want to display these check boxes by getting the list of options from the db table.

Do we need to create models for such db tables to get data?

Thanks in Advance.

you mean the models for the checkbox/radios/select box options? If you are using Active Record to retrieve them, then yes you need.

You can create the active records and use CHTml::listData for prepare the array of value->name items for the select/boxes dropdown.

For instance:


<?php $form->dropDownList($model, 'attribute', CHtml::listData(ElementModel::model()->findAll(), 'valueField', 'nameField'));?>

If you don’t need to edit that data, well no, you don’t need to as you can always use the database or a command to do that (DAO - http://www.yiiframework.com/doc/guide/1.1/en/database.dao#sec-1) but… IMHO I suggest you do create the models per table because if in a future you need to implement edition features to that table, you’ll have to create the models anyway and I think ‘gii’ does a great job for you isnt it?

I would rather create the models even if I dont really need them at the moment and use zaccaria’s approach…

@sumwai, @zaccaria, @Antonio Ramirez

Thank you.

Actually I have lot of such tables. In this case, I will have large number of model classes just for the purpose of retrieving data from the table apart from those, which have CRUD functionalities. If possible, I may have to store their data(list options) in common tables. This may reduce the number of model classes that I need for the application.

Using DAO will make a bit more complex the coding, while models are 100% gratis as they are generated by Gii.

If you think to be lazy enough, generate AR.

If you don’t need to change the value, is more confortable to use enum type in database, it will avoid you to create one more table and data will be more readable with phpmyadmin.

If you have to edit the options, is more confortable to use AR. So if you need a table, in 90% of the situation you will need even an AR

100% with you zac

I have the same set of checkboxes/radios in multiple forms. So, enum may not be a better choice for me. If I add an option later (I don’t need UI for adding these, as I don’t need to add these in general), it should work in all the forms. I should not modify the table structures anymore.

The idea of using AR for each of them is looking good but I may have lot of models holding the data of checkboxes/radios options. Actually I don’t need to have CRUD for these tables. So I can create a model, which will have methods, which retrieve data from these tables using DAO.