Yii Framework Forum: Ctimestampbehavior Malfunctioning With Afterfind() In Model - Yii Framework Forum

Jump to content

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

Ctimestampbehavior Malfunctioning With Afterfind() In Model Rate Topic: -----

#1 User is offline   Anil Konsal 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 6
  • Joined: 28-January 12

Posted 16 November 2012 - 07:57 AM

I am using CTimeStampBehaviour for setting create_time and update_time resetting create_time with afterFind() in model, Basically I am setting the format of date to be displayed on the front end. But this does not work as create_time is sent 0000-00-00 00:00:00 on using model's afterFind(). It works fine when I comment afterFind() in model.

I have used this function in my model:
public function behaviors(){
return array(
    'CTimestampBehavior' => array(
        'class' => 'zii.behaviors.CTimestampBehavior',
        'createAttribute' => 'create_time',
        'updateAttribute' => 'update_time',
        'setUpdateOnCreate'=> true,
    )
);
}

I have seen the documentation on CTimeStampBehavior class on yii site, it has a reference to afterFind() inheritence but does not suggest how to use it. I am using afterFind() in model like this:
protected function afterFind() {
parent::afterFind();
$this->create_time = Yii::app()->dateFormatter->formatDateTime(
        CDateTimeParser::parse(
            $this->create_time, 'yyyy-MM-dd hh:mm:ss'
        ), 'short', 'short'
    );
return true;
}

Can anyone suggest why the create_time is being zero filled when using afterFind()?
0

#2 User is offline   waterloomatt 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 545
  • Joined: 09-April 10

Posted 16 November 2012 - 01:23 PM

afterFind is a void method and shouldn't return anything.

Also, try something simpler to narrow down where the error is taking place.

// Not tested
public function afterFind()
{
    $this->create_time = date('F, j, Y', strtotime($this->create_time));

    parent::afterFind();
}


I do something similar to convert UTC time to my user's local time. I've implemented it as a behavior but you could do the same in the model.
// Model

    public function behaviors()
    {
        return array(
            'CTimestampBehavior' => array(
                'class' => 'zii.behaviors.CTimestampBehavior',
                'createAttribute' => 'created',
                'updateAttribute' => null,
            ),
            'TimezoneBehavior' => array(
                'class' => 'application.components.behaviors.BNDTimezoneBehavior',
                'attributes' => array(
                    'created'
                )
            ),
        );
    }


<?php
/**
 * BNDTimezoneBehavior class file.
 *
 * @author Matt Skelton
 * @date 25-Aug-2012
 */

/**
 * Coverts database datetime fields (stored as UTC) to the user's local timezone.
 */
class BNDTimezoneBehavior extends CActiveRecordBehavior
{
    public $attributes = array();

    /**
     * Converts the database's stored DateTime field into the user's local time.
     * @param CEvent $event
     */
    public function afterFind($event)
    {
        if (Yii::app() instanceof CWebApplication)
        {
            if (Yii::app()->user->getState('timezone'))
            {
                $timezone = Yii::app()->user->timezone;

                foreach ($this->attributes as $attribute)
                {
                    $date = $this->getOwner()->{$attribute};
                    $serverTimezone = new DateTimeZone("UTC");
                    $localTimezone = $timezone;

                    $date = new DateTime($date, $serverTimezone);
                    $date->setTimezone(new DateTimeZone($localTimezone));

                    $this->getOwner()->{$attribute} = $date->format('Y-m-d H:i:s');
                }
            }
        }
    }

    /**
     * Converts the user's local time back into UTC.
     * @param CEvent $event
     */
    public function beforeSave($event)
    {
        if (Yii::app() instanceof CWebApplication)
        {
            if (!$this->owner->isNewRecord)
            {
                if (Yii::app()->user->getState('timezone'))
                {
                    $timezone = Yii::app()->user->timezone;

                    foreach ($this->attributes as $attribute)
                    {
                        $date = $this->getOwner()->{$attribute};
                        $serverTimezone = new DateTimeZone("UTC");

                        $date = new DateTime($date, new DateTimeZone($timezone));
                        $date->setTimezone($serverTimezone);

                        $this->getOwner()->{$attribute} = $date->format('Y-m-d H:i:s');
                    }
                }
            }
        }
    }
}
?>

Matt
1

#3 User is offline   Anil Konsal 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 6
  • Joined: 28-January 12

Posted 21 November 2012 - 01:16 PM

Hi Matt, the code you provided in the first block did not work either!
0

#4 User is offline   waterloomatt 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 545
  • Joined: 09-April 10

Posted 22 November 2012 - 07:55 AM

What data type is the create_time field using in the DB?
0

#5 User is offline   Anil Konsal 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 6
  • Joined: 28-January 12

Posted 26 November 2012 - 08:58 AM

Its datetime.

View Postwaterloomatt, on 22 November 2012 - 07:55 AM, said:

What data type is the create_time field using in the DB?

0

#6 User is offline   waterloomatt 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 545
  • Joined: 09-April 10

Posted 27 November 2012 - 09:31 AM

Hmmm....

Please run this code and see what comes back.
public function afterFind()
{    
    echo date("Y-m-d H:i:s", strtotime($this->create_time));    
    parent::afterFind();
}


If "0000-00-0....", please post your entire model.

Thanks,

Matt
0

#7 User is offline   jbowler 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 16
  • Joined: 05-August 12

Posted 18 December 2012 - 02:39 PM

Did this ever get resolved? I think I'm seeing the same issue.
0

#8 User is offline   Anil Konsal 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 6
  • Joined: 28-January 12

Posted 19 January 2013 - 01:38 AM

View Postjbowler, on 18 December 2012 - 02:39 PM, said:

Did this ever get resolved? I think I'm seeing the same issue.


No I am still figuring out the resolution.
0

#9 User is offline   Anil Konsal 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 6
  • Joined: 28-January 12

Posted 19 January 2013 - 04:24 AM

Here is another code that I am using now. But it also does not work:

public function beforeSave() {
	if ($this->isNewRecord)
	    $this->cdate = new CDbExpression('NOW()');

	$this->mdate = new CDbExpression('NOW()');

	return parent::beforeSave();
    }

    protected function afterFind() {

	$this->cdate = Yii::app()->dateFormatter->formatDateTime(
			CDateTimeParser::parse(
				$this->cdate, 'yyyy-MM-dd hh:mm:ss'
			), 'short', 'short'

		);

	$this->mdate = Yii::app()->dateFormatter->formatDateTime(
			CDateTimeParser::parse(
				$this->mdate, 'yyyy-MM-dd hh:mm:ss'
			), 'short', 'short'

		);

	return parent::afterFind();
    }

0

#10 User is offline   francis ja 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 31
  • Joined: 27-July 11
  • Location:India

Posted 28 January 2013 - 12:04 PM

did you try using public instaead of protected
also remove return form "return parent::afterFind();"
skype: jesusloves.francis
Blogs:
http://yiideveloper.wordpress.com/
http://francis-yii-developer.me
Co-founder of Nintriva Wireless
mails:
jesusloves.francis@gmail.com
francis@nintriva.com
http://nintriva.com
0

#11 User is offline   francis ja 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 31
  • Joined: 27-July 11
  • Location:India

Posted 28 January 2013 - 12:07 PM

i think there is no need of retrun in anywhere on afterfind
skype: jesusloves.francis
Blogs:
http://yiideveloper.wordpress.com/
http://francis-yii-developer.me
Co-founder of Nintriva Wireless
mails:
jesusloves.francis@gmail.com
francis@nintriva.com
http://nintriva.com
0

#12 User is offline   samdark 

  • Having fun
  • Yii
  • Group: Yii Dev Team
  • Posts: 3,738
  • Joined: 17-January 09
  • Location:Russia

Posted 02 February 2013 - 04:33 AM

You should return true from before* in order for the method to be executed.
Yii 1.1 Application Development Cookbook

Enjoying Yii? Star us at github: 1.1 and 2.0.
0

#13 User is offline   Johnny Gan 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 198
  • Joined: 30-May 13

Posted 24 July 2013 - 11:37 AM

I have similar issue, and here is my code, and it's working in my program. For your reference.

class User extends CActiveRecord
{
...
	public function afterFind()
	{		
		parent::afterFind();
		
		$dateFields=array('user_start_date','user_end_date');
		foreach($dateFields AS $dateField)
		{			
			$dateValue = $this->attributes[$dateField];			
			if($dateValue == '12/31/1969' || $dateValue == '1969-12-31' || $dateValue == '00/00/0000' || $dateValue == '0000-00-00')
			{
				$this->setAttribute($dateField, NULL);
			}
		}			
		return true;
	}
...

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