Yii Framework Forum: TimeStampBehavior example - Yii Framework Forum

Jump to content

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

TimeStampBehavior example TimeStampBehavior Rate Topic: -----

#1 User is offline   fourcs 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 17
  • Joined: 16-April 09

Posted 28 July 2010 - 12:05 PM

I have two fields, dateCreated, dateChanged. They are both timestamp fields. I want to use the TimeStampBehavior in the controller to fill these fields automatically. I've placed this code in the controller:
public function behaviors(){
	return array(
		'CTimestampBehavior' => array(
			'class' => 'zii.behaviors.CTimestampBehavior',
			'createAttribute' => 'create_time_attribute',
			'updateAttribute' => 'update_time_attribute',
		)
	);
}


I've not been successful in having the two fields filled automatically. :angry: Any help will be appreciated.
0

#2 User is online   tri 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,651
  • Joined: 20-November 08
  • Location:Stockholm, Sweden

Posted 28 July 2010 - 04:08 PM

View Postfourcs, on 28 July 2010 - 12:05 PM, said:

I have two fields, dateCreated, dateChanged. They are both timestamp fields. I want to use the TimeStampBehavior in the controller to fill these fields automatically. I've placed this code in the controller:
public function behaviors(){
	return array(
		'CTimestampBehavior' => array(
			'class' => 'zii.behaviors.CTimestampBehavior',
			'createAttribute' => 'create_time_attribute',
			'updateAttribute' => 'update_time_attribute',
		)
	);
}


I've not been successful in having the two fields filled automatically. :angry: Any help will be appreciated.


Explained here:
http://www.yiiframew...mestampBehavior

You have to create the behavior() method in your model.

/Tommy
Don't forget to read The Definitive Guide to Yii (en) (sv) | The class reference has the details
0

#3 User is offline   fourcs 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 17
  • Joined: 16-April 09

Posted 29 July 2010 - 01:50 AM

Yeah, I've tried that. I've used the same code in my model with no success.
0

#4 User is offline   Maurizio Domba Cerin 

  • Yii - Yesss It Is !!!
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,359
  • Joined: 12-October 09
  • Location:Croatia

Posted 29 July 2010 - 02:09 AM

The createAttribute and updateAttribute you have to set to your fields like:

public function behaviors(){
   return array(
      'CTimestampbehavior' => array(
         'class' => 'zii.behaviors.CTimestampbehavior',
         'createAttribute' => 'dateCreated',
         'updateAttribute' => 'dateChanged',
      )
   );
}


note that "B" in "behavior" in CTimestampBehavior should be "Behavior"... this forum converts it to lower letter
Find more about me.... btw. Do you know your WAN IP?
0

#5 User is offline   fourcs 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 17
  • Joined: 16-April 09

Posted 29 July 2010 - 05:02 AM

View Postmdomba, on 29 July 2010 - 02:09 AM, said:

The createAttribute and updateAttribute you have to set to your fields like:

public function behaviors(){
   return array(
      'CTimestampbehavior' => array(
         'class' => 'zii.behaviors.CTimestampbehavior',
         'createAttribute' => 'dateCreated',
         'updateAttribute' => 'dateChanged',
      )
   );
}


note that "B" in "behavior" in CTimestampBehavior should be "Behavior"... this forum converts it to lower letter


Works like a charm :) Can't thank you enough!
0

#6 User is offline   tihanyilaci 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 29
  • Joined: 29-July 11

Posted 26 September 2013 - 02:04 AM

Hi Maurizio,

I would like to ask about this behavior. I set the timezone in the index.php file in my yii project. Unfortunately this TimestampBehavior does not take care this timezone setting. How can i set the timezone in the behavior ?

Thanks

Laszlo from Hungary

View PostMaurizio Domba, on 29 July 2010 - 02:09 AM, said:

The createAttribute and updateAttribute you have to set to your fields like:

public function behaviors(){
   return array(
      'CTimestampbehavior' => array(
         'class' => 'zii.behaviors.CTimestampbehavior',
         'createAttribute' => 'dateCreated',
         'updateAttribute' => 'dateChanged',
      )
   );
}


note that "B" in "behavior" in CTimestampBehavior should be "Behavior"... this forum converts it to lower letter

0

#7 User is offline   Maurizio Domba Cerin 

  • Yii - Yesss It Is !!!
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,359
  • Joined: 12-October 09
  • Location:Croatia

Posted 26 September 2013 - 02:59 AM

if you check the source code you will see that this behavior sets the value of the fields to CDbExpression('NOW()')... so it's the database (not the PHP script) that sets the date...
Find more about me.... btw. Do you know your WAN IP?
0

#8 User is offline   tihanyilaci 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 29
  • Joined: 29-July 11

Posted 26 September 2013 - 03:23 AM

oops i should have check that

thanks anyway, have a nice day!

View PostMaurizio Domba, on 26 September 2013 - 02:59 AM, said:

if you check the source code you will see that this behavior sets the value of the fields to CDbExpression('NOW()')... so it's the database (not the PHP script) that sets the date...

0

#9 User is offline   tihanyilaci 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 29
  • Joined: 29-July 11

Posted 26 September 2013 - 03:40 AM

if it just would be useful for someone, i solved my problem:

You have to set an attribute when calling the behavior and the timestamp is set by php now:

'CTimestampBehavior' => array(
                'class' => 'zii.behaviors.CTimestampBehavior',
                'createAttribute' => 'created',
                'updateAttribute' => 'modified',
                'timestampExpression' => 'date("Y-m-d H:i:s")',
                'setUpdateOnCreate' => true,
            ),




View Posttihanyilaci, on 26 September 2013 - 03:23 AM, said:

oops i should have check that

thanks anyway, have a nice day!

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