3 DropDownList to send data for one database field

Hi folks,

I need help to create a view, I have a database field called date_to_show,

that user need choose a year-month-day using dropdowns.

I need know how can I get this data of three separeted dropdowns fields

(year|month|day) and store in a single date field of database?

If not, what is the better form of make this?

Thanks.

Hi,

you can create 3 dropdowns in your view and then join their values to one string in your controller. Then use CDateTimeParser to convert date string to UNIX timestamp. When retrieving date from db use CDateFormatter to convert timestamp to date string again.

As an alternative you can store date in date type in your db. Its useful if you have to do some complex comparisons or manipulate fields values (add 3 days, subtract 1 month, find last day of month, and so on). Keep in mind that date field require a specific format. For MySql check this.

Hi, I tried to do this, for create all work but when I started update a record the fields back

with prompt empty and if I select a date again the validation return a

error how if the fields were blank.

How can I fix this?

It’s hard to say exactly why your update fail without seeing the source code, but I assume it’s a problem with binding db’s date field to your model attributes. I would start with creating three attributes in model. Lets say dDate, mDate and yDate. This attributes should be added to safeAttributes array and to the rules array and used in view to generate dropdowns instead of ‘original’ attribute which is your date field in db (let’s say oDate). Now events could be useful. I would use 2, onBeforeSave and onAfterFind. In first event callback dDate, mDate and yDate should be converted to timestamp or date string and assigned to oDate. In onAfterFind event callback oDate value should be split and values assigned to dDate, mDate and yDate. Neither rules nor safeAttributes array should contain any references to oDate. Validate and work only with your 3 custom attributes. That’s my opinion on how I would do that.

As an alternative you can use CMaskedTextField see demo here, or datePicker.

I hope some of that will help you :)