Skip to content

Instantly share code, notes, and snippets.

@rpastorelle
Last active December 15, 2015 14:19
Show Gist options
  • Save rpastorelle/5273647 to your computer and use it in GitHub Desktop.
Save rpastorelle/5273647 to your computer and use it in GitHub Desktop.
Sample DB Model Class
<?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