function __construct(TableReferenceInstance $table) { $this->table = $table; $this->tableInfo = $table->Info(); $structure = CreateTable::fromTable($table); $this->engine = $structure->engine; //Work out which fields are IDs if (isset($structure->indexes['PRIMARY'])) { $this->id = $structure->indexes['PRIMARY']->getKeys(); } //Build mapping translation array $this->mappings = $this->getMappings($structure)->translationArray(); //This is the auto increment field, if it exists foreach ($this->id as $col) { if ($structure[$col]->hasAttribute('AUTO_INCREMENT')) { //Store the auto increment field in ORM format $this->autoIncrement = $this->mappings[$col]; $this->autoIncrementField = $col; //There can only be one AUTO_INCREMENT field per table (also it must be in the PKey) break; } } //build relation array if ($this->engine == 'innodb') { foreach ($structure->relations as $r) { $reference = $r->getReference(); $this->relations[$r->getField()] = $reference; //$mapper = new Mappings($reference->getTableReference()->info(),CreateTable::fromTable($reference->getTable())); //$this->relationReverseMappings[$r->getField()] = $mapper->translateToObjective($reference->getColumn()); } } elseif ($this->engine == 'myisam') { $this->relations = MyIsam::fieldReferences($structure, $this->table); } else { throw new \Exception('Unknown database engine type: ' . $this->engine); } //Work out reverse references $tableName = $this->tableInfo['name']; foreach (TableReference::getAll() as $ref) { if ($ref->exists()) { $rStruct = CreateTable::fromTable($ref->getTable()); foreach ($rStruct->relations as $relation) { $reference = $relation->getReference(); $rTable = $reference->getTable(); if ($rTable == $tableName) { $this->references[] = array('from_table' => $ref, 'from_field' => $relation->getField(), 'to_field' => $reference->getColumn()); } } } } //Dnamic Typing data $this->dynamicTyping = new DynamicTyping\Instance($table); $this->dynamicTyping = $this->dynamicTyping->map; //Validation $this->validation = new Validation($structure, $this->dynamicTyping); parent::__construct($this->mappings); //Store into cache Cache::Set($table, $this); }
static function getModel(TableReferenceInstance $table, $data = true) { //cached $model = Cache::Get($table); if ($model) { return $model; } if (!$table->exists()) { return false; } $model = new Model($table); if ($data) { $model = $model->toModelData(); } return $model; }
static function init() { self::$pool = \Radical\Cache\PooledCache::get('radical_orm', 'Memory'); global $_SQL; $cfile = '/tmp/' . $_SQL->getDb(); if (file_exists($cfile) && filemtime($cfile) >= time() - 30) { self::$key = file_get_contents($cfile); } else { touch($cfile); $sql = 'SELECT MAX(UNIX_TIMESTAMP( CREATE_TIME )) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = "' . $_SQL->getDb() . '"'; self::$key = \Radical\DB::Q($sql)->Fetch(Fetch::FIRST); file_put_contents($cfile, self::$key); } if (Server::isProduction()) { self::$data = self::$pool->get($_SQL->getDb() . '_' . self::$key); register_shutdown_function(function () { Cache::save(); }); } if (!is_array(self::$data)) { self::$data = array(); } }