/** * Initializes the ShoppDatabaseObject with functional necessities * * A ShoppDatabaseObject tracks meta data relevant to translating PHP object * data into SQL-ready data. This is done by reading and caching the * table schema so the properties and their data types can be known * in order to automate query building. * * The table schema is stored in an array structure that contains * the columns and their datatypes. This structure is cached as the * current data_model setting. If a table is missing from the data_model * a new table schema structure is generated on the fly. * * @author Jonathan Davis * @since 1.0 * * @param string $table The base table name (without prefixes) * @param string $key (optional) The column name of the primary key * @return boolean True if init was successful, otherwise false **/ public function init($table, $key = null) { if (is_null($key)) { $key = 'id'; } $Settings = ShoppSettings(); // So we know what the table name is if (!empty($table) && (!isset($this->_table) || empty($this->_table))) { $this->_table = $this->tablename($table); } if (empty($this->_table)) { return false; } $this->_key = $key; // So we know what the primary key is $this->_datatypes = array(); // So we know the format of the table $this->_lists = array(); // So we know the options for each list $defaults = array(); // So we know the default values for each field $map = !empty($this->_map) ? array_flip($this->_map) : array(); $Tables = $Settings->available() ? $Settings->get('data_model') : array(); if (isset($Tables[$this->_table])) { $this->_datatypes = $Tables[$this->_table]->_datatypes; $this->_lists = $Tables[$this->_table]->_lists; $defaults = $Tables[$this->_table]->_defaults; foreach ($this->_datatypes as $var => $type) { $property = isset($map[$var]) ? $map[$var] : $var; if (!isset($this->{$property})) { $this->{$property} = isset($defaults[$var]) ? $defaults[$var] : ''; } if ('date' == $type && ('0000-00-00 00:00:00' == $this->{$property} || empty($this->{$property}))) { $this->{$property} = null; } } return true; } if (!($r = sDB::query("SHOW COLUMNS FROM {$this->_table}", 'array'))) { return false; } // Map out the table definition into our data structure foreach ($r as $object) { $var = $object->Field; $this->_datatypes[$var] = sDB::datatype($object->Type); $this->_defaults[$var] = $object->Default; // Grab out options from list fields if ('list' == sDB::datatype($object->Type)) { $values = str_replace("','", ",", substr($object->Type, strpos($object->Type, "'") + 1, -2)); $this->_lists[$var] = explode(",", $values); } if (!empty($map) && !isset($map[$var])) { continue; } // Remap properties if a property map is available $property = isset($map[$var]) ? $map[$var] : $var; if (!isset($this->{$property})) { $this->{$property} = $this->_defaults[$var]; } } if ($Settings->available()) { $Tables[$this->_table] = new StdClass(); $Tables[$this->_table]->_datatypes =& $this->_datatypes; $Tables[$this->_table]->_lists =& $this->_lists; $Tables[$this->_table]->_defaults =& $this->_defaults; $Settings->save('data_model', $Tables); } return true; }