Quantcast
Channel: Yii Framework Forum
Viewing all articles
Browse latest Browse all 18717

Render Last Update Date/user And Created By User/time

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

: notupdated.png

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

: has been updated.png



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.

Viewing all articles
Browse latest Browse all 18717

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>