/** * PHP5 Constructor method */ public function __construct($values = array()) { $this->db = Db::getInstance(); // if either rowClass or $values are empty, then we just want // an empty model. Return out, nothing else to do. if (!$this->rowClass || empty($values)) { return; } try { $tableDataColumns = $this->db->query("DESCRIBE {$this->table}"); if (!$tableDataColumns) { throw new \Exception("Table not defined or found."); } } catch (\Exception $e) { die($e->getMessage()); } $this->tableData = $tableDataColumns; $this->row = new $this->rowClass(); foreach ($tableDataColumns as $column) { $field = lcfirst(str_replace(' ', '', ucwords(str_replace('_', ' ', $column['Field'])))); if (!property_exists($this->row, $field)) { $this->{$field} = null; } } foreach ($values as $property => $value) { $method = "set{$property}"; if (method_exists($this->row, $method)) { $this->row->{$method}($value); } } }
/** * Do things to close down the app on shutdown */ function shutdown() { $db = \GreenMvc\Db\Db::getInstance(); $db->close(); if (DEBUG && IS_CLI) { echo "[0;31mk[1;33mt[0;32mh[0;34mn[0;35mx[1;31mb[1;33my[1;32me[0m" . PHP_EOL; } }