Skip to content

Instantly share code, notes, and snippets.

@acodesmith
Created December 20, 2016 20:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save acodesmith/f47efd3406bbb7c5c594331184c45276 to your computer and use it in GitHub Desktop.
Save acodesmith/f47efd3406bbb7c5c594331184c45276 to your computer and use it in GitHub Desktop.
<?php
namespace app\components;
use Yii;
/**
* Class BaseModel
* @package app\models
* @property integer created
* @property integer updated
*/
class BaseModel extends \yii\db\ActiveRecord
{
/**
* Check for created and updated flags
* @param bool $insert
* @return bool
*
* @todo refactor with behaviors
* http://www.yiiframework.com/doc-2.0/yii-behaviors-timestampbehavior.html
*/
public function beforeSave($insert)
{
if (parent::beforeSave($insert)) {
if( $this->hasAttribute( 'created' ) && empty( $this->created ) ) {
$this->created = time();
}
if( $this->hasAttribute( 'updated' ) ) {
$this->updated = time();
}
return true;
} else {
return false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment