Created
December 20, 2016 20:28
-
-
Save acodesmith/f47efd3406bbb7c5c594331184c45276 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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