Store 3 Input Fields (Date: Year, Month, Day) Into 1 Database Field ('date')

I would like to store 3 input fields in 1 mysql database field, named ‘date’. In stead of storing a date in the yyyy-mm-dd format, it only stores the word ‘array’… Can anybody help?

View


<? echo CHtml::activeDropDownList($page, 'date[day]', CHtml::listData($day, 'id', 'name')); ?>

<? echo CHtml::activeDropDownList($page, 'date[month]', CHtml::listData($month, 'id', 'name')); ?>

<? echo CHtml::activeDropDownList($page, 'date[year]', CHtml::listData($year, 'id', 'name')); ?>



Controller


public function actionPage() 

{

  $page = new Page;

		

		if(isset($_POST['Page']))

		{

			

			$page->birthdate = implode("-", $_POST['Page']['date']);	

			$page->attributes = $_POST['Page'];

			$page->save();

		}

				

	}

Hi,

I know it’s not exactly your question, but why not using CJuiDatePicker extension and have only 1 field instead of 3?

change the order lines

$page->attributes = $_POST[‘Page’];

$page->birthdate = implode("-", $_POST[‘Page’][‘date’]);

$page->save();

the $page->attributes store all the post form and arese all the previus data. Also check if the implode("-", $_POST[‘Page’][‘date’]) has the data that you want, by using echo

date is a reserved column name in mysql

You can solve this in the model:




<?php

class User extends CActiveRecord

{

    // In DB we only have "dateofbirth" as DATE column.

    // We add some extra columns here:

    public $day;

    public $month;

    public $year;


    /**

 	* Prepare day/month/year for birthday dropdown

 	*/

    public function afterFind()

    {

        $ts = strtotime($this->dateofbirth);

        list($this->day,$this->month,$this->year) = array(date('j',$ts), date('n',$ts), date('Y',$ts));

    }


    /**

 	* Convert day/month/year back to a date string for dateofbirth

 	*/

    public function beforeSave()

    {

        // Create timestamp of entered date

        $ts = strtotime(sprintf('%s-%s-%s',$this->year,$this->month,$this->day));

        $this->dateofbirth = date('d.m.Y',$ts);

    }

}

EDIT: Don’t forget to add rules for “day”, “month” and “year”.