Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * @brief Base class for loading every module
- *
- * Handles automatic loading of properties, provides interface to
- * verify values
- * NEVER instantiate this class directly
- **/
- class Loadable {
- protected $init = false;
- protected $required = array();
- protected $id;
- protected $columns = array();
- protected $columntypes = array();
- protected $tablename;
- protected $dataobject;
- protected $db;
- public function __construct($firstparam = null, $connection = null) {
- if ($connection === null) {
- $connection = "default";
- }
- $this->db = \yadaal\GetConnection($connection);
- if (is_null($firstparam)) {
- return;
- }
- $this->init = true;
- if (is_array($firstparam)) {
- $this->insert($firstparam);
- return;
- }
- else if(is_int($firstparam) || is_string($firstparam)) {
- $this->init($firstparam);
- return;
- }
- }
- public function insert(array $data) {
- $table = $this->db->Table($this->tablename);
- $cont = true;
- foreach($required as $colname) {
- if (!isset($data[$colname])) {
- $cont = false;
- }
- }
- $id = $table->insert($data);
- $this->init($id);
- }
- public function init($id) {
- $table = $this->db->Table($this->tablename);
- $idcol = $this->id;
- $table->$idcol = $id;
- if ($table->numRows == 0) {
- $this->init = false;
- return;
- }
- $this->dataobject = $table->result();
- }
- public function __get($name) {
- return $dataobject->$name;
- }
- public function __set($name, $value) {
- if ($this->validate($name, $value)) {
- $this->dataobject->$name = $this->transform($name, $value);
- }
- }
- public function validate($name, $value) {
- return true;
- }
- public function transform($name, $value) {
- return $value;
- }
- public function isEqual($name, $value) {
- return $this->__get($name) == $value;
- }
- public function isStrictlyEqual($name, $value) {
- return $this->__get($name) === $value;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment