Last active
December 15, 2015 14:19
-
-
Save rpastorelle/5273647 to your computer and use it in GitHub Desktop.
Sample DB Model Class
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 | |
// Extending from "Model" | |
class DBTableName extends Model { | |
// -------------------------- | |
// PROPERTIES | |
// -------------------------- | |
/** | |
* schema must be defined. array of fields in the DB | |
**/ | |
protected $schema = array('id','name','value','created','modified'); | |
// Add additional properties here... | |
// -------------------------- | |
// CONSTRUCTOR | |
// -------------------------- | |
function __construct( $id = null ){ | |
global $CONFIG, $TackkRuntime; | |
// Call on the Model constructor: | |
parent::__construct( $CONFIG['private']['table_<tableName>'] ); | |
// Load values from db: | |
if( isset($id) ) $this->load( 'id=?', $id ); | |
// $this->loadAdditionalProperties(); | |
} | |
// -------------------------- | |
// METHODS | |
// -------------------------- | |
// Overload the save method to set some defaults: | |
public function save(){ | |
// Update dates: | |
$curtime = time(); | |
if( $this->dry() ){ | |
// dry == new | |
$this->created = $curtime; | |
} | |
$this->modified = $curtime; | |
// Call Model Save | |
parent::save(); | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment