Exemplo n.º 1
0
 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);
 }
Exemplo n.º 2
0
 function getMappings($structure = null)
 {
     //Accept a null strucure for when this method is called externally
     if ($structure === null) {
         $structure = CreateTable::fromTable($this->table);
     }
     //Return a Mapping Manager
     return new Mappings($this->tableInfo, $structure);
 }
Exemplo n.º 3
0
 function join($table, $alias, $on = null, $type = 'left')
 {
     $where = $on;
     if (!is_string($where) && !$where instanceof IToSQL) {
         if ($where === null) {
             //Automatically detirmine linkage using foreign keys
             $ct = CreateTable::fromTable($table);
             foreach ($ct->relations as $rk => $relation) {
                 $reference = $relation->getReference();
                 //TODO: Check against other joins
                 $rightAlias = $this->getTableAlias($reference->getTable());
                 if ($rightAlias) {
                     $where = array(array($alias, $relation->getField()), array($rightAlias, $reference->getColumn()));
                 }
             }
         }
         if (is_array($where)) {
             //array('leftSide','rightSide');
             //or array('leftSide','=','rightSide')
             //WHERE
             //leftSide = array('alias','field')
             //or 'leftSide'
             if (count($where) == 2) {
                 $where = array($where[0], '=', $where[1]);
             }
             //Resolve out arrayed members
             foreach ($where as $k => $v) {
                 if (is_array($v)) {
                     $where[$k] = new TableExpression($v[1], $v[0]);
                 }
             }
             $where = implode(' ', $where);
         }
     }
     $class = '\\Radical\\Database\\SQL\\Parts\\Join\\' . ucfirst($type) . 'Join';
     $this->joins[$alias] = new $class($table . ' ' . $alias, $where);
     return $this;
 }