function filter($text, SelectStatement $sql, $table)
 {
     $table = TableReference::getByTableName($table);
     if ($table === null) {
         throw new \Exception('Couldnt find table model "' . $table . '" to search');
     }
     $where = new WhereOR();
     $useWhere = false;
     $k = null;
     $i = array();
     $rows = $this->Search($text, $table);
     foreach ($rows as $row) {
         if (count($row) == 1) {
             $a = array_keys($row->toArray());
             $b = array_values($row->toArray());
             $k = $a[0];
             $i[] = $b[0];
         } else {
             $where->Add(new Where($row));
             $useWhere = true;
         }
     }
     if ($useWhere) {
         $sql->where_and($where);
     } else {
         $in = new In($i);
         $in = new Comparison($k, $in, '');
         $sql->where_and($in);
     }
 }
示例#2
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);
 }
示例#3
0
 /**
  * @param $field
  * @return TableReferenceInstance
  */
 static function find($field)
 {
     $prefixLen = 0;
     $ref = null;
     foreach (TableReference::getAll() as $table) {
         $prefix = $table->getPrefix();
         $cpLen = strlen($prefix);
         if ($prefixLen < $cpLen) {
             if (substr_compare($field, $prefix, 0, $cpLen) == 0) {
                 $prefixLen = $cpLen;
                 $ref = $table;
             }
         }
     }
     return $ref;
 }
示例#4
0
 /**
  * Gets a row from ID.
  * If the primary key spans multiple columns then accepts
  * input only as an of column => value etc `array('key_name1'=> ...)`
  * Else also accepts input as a scalar value
  * 
  * ```
  * $post = Post::fromId(1);
  * ```
  * 
  * @param mixed $id
  * @throws \Exception
  * @return static
  */
 static function fromId($id, $forUpdate = false)
 {
     $orm = ORM\Manager::getModel(TableReference::getByTableClass(get_called_class()));
     //Base SQL
     $sql = static::_select();
     if ($forUpdate) {
         $sql->for_update($forUpdate);
     }
     //Build
     if ($id instanceof Parts\Where) {
         $sql->where($id);
     } else {
         $idk = $orm->id;
         if (is_array($id)) {
             if (count($id) != count($idk)) {
                 throw new \Exception('Number of inputs doesnt match ' . count($id) . ' != ' . count($idk));
             }
             if (isset($id[0])) {
                 $idNew = array();
                 foreach ($idk as $k => $v) {
                     $idk[$k] = $v;
                 }
                 $id = $idk;
             }
             $sql = static::_fromFields($id, $forUpdate);
         } else {
             //Input = String, Needed Array
             if (count($orm->id) > 1) {
                 throw new \Exception('Needs more than one value for the ID of ' . $orm->tableInfo['name']);
             }
             $sql->where(array($idk[0] => $id));
         }
     }
     $res = \Radical\DB::Query($sql);
     $row = $res->Fetch();
     if ($row) {
         return new static($row);
     }
 }
示例#5
0
 function getTableReference()
 {
     return TableReference::getByTableName($this->table);
 }
示例#6
0
 function tableExists($table)
 {
     return TableReference::getByTableClass(__CLASS)->exists();
 }