Yii Framework Forum: Render Last Update Date/user And Created By User/time - Yii Framework Forum

Jump to content

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

Render Last Update Date/user And Created By User/time Is there a better way to handel this? Rate Topic: -----

#1 User is offline   skworden 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 174
  • Joined: 14-June 12
  • Location:Virginia, USA

Posted 26 January 2013 - 10:23 AM

I came up with this yesterday after poking around with it for a few minutes. I don't know if this is the best way to keep track of create / last updated information.

In my model I have:


....relations are defined like they should be.....


	public function beforeSave()
		{
			if ($this->getIsNewRecord())
			{
				$this->create_time = new CDbExpression('NOW()');
				$this->creator_id = Yii::app()->user->id;
				$this->update_time = new CDbExpression('NULL'); //set to null so it won't show the default jan, 1, 1970 1:00 if it's not set in my view.. now it will render what I want it to say see image.
				$this->update_id = new CDbExpression('NULL'); //set to null because I kept getting update_id not defined if it wasn't updated even though it's set to null in my db same w/above.
			}
			else {
			    $this->update_time = new CDbExpression('NOW()');
			    $this->update_id = Yii::app()->user->id;
			}
		return parent::beforeSave();
	}


In my view I have

<?php $this->widget('zii.widgets.CDetailView', array(
	'data'=>$model,
	'htmlOptions'=>array('class'=>'table table-striped table-bordered table-hover'),
	'attributes'=>array(
                ....other attributes

		array('name'=>'creator_id',
		      'value'=>$model->creator->username),
		array(
			'name' => 'create_time',
			'value'=>date("M-d-Y h:i:A",strtotime($model->create_time)),
		),
		array('name'=>'update_id',
			'value'=>$model->update_id==null ? 'Has not been updated' : $model->update->username),
		array(
			'name' => 'update_time',
			'value'=>$model->update_time==null ? 'Has not been updated' :date("M-d-Y h:i:A",strtotime($model->update_time)),
		),
	),
)); ?>


database I have

  `creator_id` int(12) DEFAULT NULL,
  `create_time` timestamp NULL DEFAULT NULL,
  `update_id` int(11) DEFAULT NULL,
  `update_time` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `creator_id` (`creator_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8;


Here's what it looks like when the item has NOT been updated.

Attached File  notupdated.png (8.93K)
Number of downloads: 6

Here's what it looks like when the item HAS been updated.

Attached File  has been updated.png (9.2K)
Number of downloads: 5



This works I just don't know if it's the best way to do this as I have only been coding PHP/Yii for a short period of time (about 6 months) and would like to learn the right way vs. a way that just works.

I have Googled and searched a lot about the dates and it seems to be pretty controversial with Yii on how to do it.

Any input would be appreciated.
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