Last active
December 27, 2015 11:39
-
-
Save pstjvn/7319756 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 | |
/** | |
* Packs some useful methods to be used in the Application models. | |
*/ | |
class App extends Database { | |
protected $_uniqueKey; | |
public function __construct() { | |
$this->_uniqueKey = 'id'; | |
parent::__construct(); | |
} | |
/** | |
* Getter for the unique key (the ID of the record in DB). | |
* @return {string} | |
*/ | |
public function getUKey() { | |
return $this->_uniqueKey; | |
} | |
///////////////// STATIC METHODS /////////////////// | |
/** | |
* Override this method for each new model class to allow static methods | |
* to work correctly. | |
* @return {string} The class name late bound. | |
*/ | |
public static function getClassName() { | |
return __CLASS__; | |
} | |
/** | |
* Gets a unique record (by its prmary unique key). | |
* @return {?Class} Returns instance of the class the method was invoked from. | |
*/ | |
public static function getById($id, $where='') { | |
$classname = static::getClassName(); | |
$instance = new $classname(); | |
$result = $instance->select() | |
->where($instance->getUKey() . ' = ? ' . $where) | |
->prepare(array($id)); | |
if (!empty($result)) { | |
return $result[0]; | |
} else { | |
return null; | |
} | |
} | |
} | |
/** | |
* Extending class, most often we would want to use it as | |
* Test::getById(1); | |
* and we would expect instance of the class (Test). | |
*/ | |
class Test extends App { | |
public function __construct() { | |
$this->object_of = "Test"; | |
$this->table = "or_vendors"; | |
parent::__construct(); | |
$this->_uniqueKey = 'vnd_id'; | |
} | |
/////////////////// STATIC METHODS ////////////////// | |
/** @override */ | |
public static function getClassName() { | |
return __CLASS__; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment