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

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

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

#1 User is offline   ronxjansen 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 09-July 12

Posted 10 December 2012 - 08:31 AM

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();
		}
				
	}

0

#2 User is offline   ragua 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 135
  • Joined: 23-January 12
  • Location:Paris - France

Posted 10 December 2012 - 10:27 AM

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

#3 User is offline   Kostas Apazidis (KonApaz) 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,332
  • Joined: 21-February 11
  • Location:Greece

Posted 10 December 2012 - 02:15 PM

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
Yii is the best php framework in the world!
It is also powerful and flexible for large scale websites
find our demo Yii extension on www.webkit.gr
Is it post useful? please v++ ;)
0

#4 User is offline   Dick Brouwers 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 6
  • Joined: 29-June 11

Posted 10 December 2012 - 06:33 PM

date is a reserved column name in mysql
0

#5 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,016
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 11 December 2012 - 03:14 AM

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".
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users