Exemple #1
0
 public function map()
 {
     $query = "SHOW COLUMNS FROM {$this->database}.{$this->table}";
     $res = $this->fetch($query);
     if (!count($res)) {
         $sql = "CREATE TABLE {$this->database}.{$this->table} (\n                  `id` int(11) unsigned NOT NULL AUTO_INCREMENT PRIMARY KEY,\n                  `created_at` datetime NOT NULL,\n                  `updated_at` datetime NOT NULL\n                ) COMMENT='Auto generated table {$this->table}' ENGINE='InnoDB' COLLATE 'utf8_general_ci';";
         $this->db->query($sql);
         $query = "SHOW COLUMNS FROM {$this->database}.{$this->table}";
         $res = $this->fetch($query);
     }
     $settings = isAke(self::$config, "{$this->database}.{$this->table}");
     $relations = isAke($settings, 'relations', false);
     if (false === $relations) {
         $relations = array();
         $relationsQuery = "SELECT\n\n                REFERENCED_TABLE_NAME as foreignTable\n                FROM information_schema.REFERENTIAL_CONSTRAINTS\n                WHERE\n                UNIQUE_CONSTRAINT_SCHEMA = '{$this->database}'\n                AND TABLE_NAME = '{$this->table}'";
         $resRel = $this->fetch($relationsQuery);
         if (count($resRel)) {
             foreach ($resRel as $rowRel) {
                 array_push($relations, $rowRel['foreignTable']);
             }
         }
         self::$config["{$this->database}.{$this->table}"]['relations'] = $relations;
     }
     $fields = $nullable = $keys = $default = array();
     $pk = null;
     if (count($res)) {
         foreach ($res as $row) {
             $fields[$row['Field']] = array('type' => typeSql($row['Type']));
             $nullable[$row['Field']] = 'yes' == Inflector::lower($row['Null']) ? true : false;
             $default[$row['Field']] = is_null($row['Default']) ? 'null' : $row['Default'];
             if ($row['Key'] == 'PRI') {
                 $pk = $row['Field'];
             }
             if ($row['Key'] != 'PRI' && strlen($row['Key'])) {
                 array_push($keys, $row['Field']);
             }
         }
     }
     $this->map = array('fields' => $fields, 'nullable' => $nullable, 'default' => $default, 'pk' => $pk, 'keys' => $keys);
     if (false === $relations) {
         $relations = array();
     }
     if (count($keys)) {
         foreach ($keys as $key) {
             if (strstr($key, '_id')) {
                 $fkField = repl('_id', '', $key);
                 if (!Arrays::in($fkField, $relations)) {
                     array_push($relations, $fkField);
                 }
             }
         }
     }
     self::$config["{$this->database}.{$this->table}"]['relations'] = $relations;
     return $this;
 }
Exemple #2
0
 private function map()
 {
     $q = "SHOW COLUMNS FROM {$this->table}";
     $res = $this->query($q, false);
     if (empty($res)) {
         throw new Exception("The system cannot access to the table {$this->table} on {$this->db}.");
     }
     $conf = $pks = $keys = [];
     foreach ($res as $data) {
         $field = $data['Field'];
         $conf[$field] = [];
         $conf[$field]['type'] = typeSql($data['Type']);
         $conf[$field]['nullable'] = 'yes' == Inflector::lower($data['Null']) ? true : false;
         if ($data['Key'] == 'PRI') {
             array_push($pks, $field);
         }
         if (strlen($data['Key']) && $data['Key'] != 'PRI') {
             array_push($keys, $field);
         }
     }
     $this->pks = $pks;
     $this->keys = $keys;
     $this->fields = $conf;
 }
Exemple #3
0
 protected function map()
 {
     $_start = $this->_getTime();
     if (!ake('relationshipEntities', $this->_datas['configModel'])) {
         $this->_datas['configModel']['relationshipEntities'] = array();
     }
     $q = "SHOW COLUMNS FROM {$this->_tableName}";
     $key = sha1($q . $this->_dbName);
     $maps = Utils::get('ModelsMap');
     $maps = null === $maps ? array() : $maps;
     if (ake($key, $maps)) {
         $res = $maps[$key];
         $count = count($res);
     } else {
         $res = $this->_query($q);
         $cols = array();
         if (is_array($res)) {
             $count = count($res);
         } else {
             $count = $res->rowCount();
         }
     }
     if (false === $res) {
         throw new Exception("This table {$this->_table} doesn't exist in {$this->_entity} entity.");
     }
     if ($count > 0) {
         foreach ($res as $row) {
             $cols[] = $row;
             $field = $row['Field'];
             $this->_datas['fields'][] = $field;
             $this->_datas['fieldsSave'][] = $field;
             $this->_datas['type'][$field] = typeSql($row['Type']);
             $this->_datas['isNullable'][$field] = 'yes' == Inflector::lower($row['Null']) ? true : false;
             $this->{$field} = null;
             if ($row['Key'] == 'PRI') {
                 $this->_datas['pk'] = $field;
                 $this->_datas['pks'][] = $field;
             }
             if (strlen($row['Key']) && $row['Key'] != 'PRI') {
                 $this->_datas['keys'][] = $field;
             }
         }
         if (ake('pk', $this->_datas)) {
             if (null === $this->_datas['pk']) {
                 if (Arrays::in($this->_table . '_id', $this->_datas['fields'])) {
                     $this->_datas['pk'] = $this->_table . '_id';
                     $this->_datas['pks'][] = $this->_table . '_id';
                 }
             }
         } else {
             if (Arrays::in($this->_table . '_id', $this->_datas['fields'])) {
                 $this->_datas['pk'] = $this->_table . '_id';
                 $this->_datas['pks'][] = $this->_table . '_id';
             }
         }
         if (ake('keys', $this->_datas)) {
             if (!count($this->_datas['keys'])) {
                 foreach ($this->_datas['fields'] as $field) {
                     $isId = ake($field, $this->_datas['configModel']['relationship']);
                     if (true === $isId) {
                         if ($field != $this->_datas['pk']) {
                             $this->_datas['keys'][] = $field;
                         }
                     }
                 }
             }
         } else {
             foreach ($this->_datas['fields'] as $field) {
                 $isId = ake($field, $this->_datas['configModel']['relationship']);
                 if (true === $isId) {
                     if ($field != $this->_datas['pk']) {
                         $this->_datas['keys'][] = $field;
                     }
                 }
             }
         }
         if (ake('relationship', $this->_datas['configModel'])) {
             foreach ($this->_datas['configModel']['relationship'] as $field => $relationship) {
                 if (!Arrays::in($field, $this->_datas['fields'])) {
                     $this->_datas['fields'][] = $field;
                     $this->_datas['keys'][] = $field;
                     if ($relationship['type'] != 'oneToMany' && $relationship['type'] != 'manyToMany') {
                         if (ake($field, $this->_datas['configModel']['relationshipEntities'])) {
                             $entity = $this->_datas['configModel']['relationshipEntities'][$field];
                         } else {
                             $entity = $this->_entity;
                         }
                         $this->_datas['models'][$field] = new self($entity, $field);
                     }
                 }
             }
         }
     }
     if (ake('fields', $this->_datas)) {
         $this->_datas['fields'] = array_unique($this->_datas['fields']);
     }
     if (ake('keys', $this->_datas)) {
         $this->_datas['keys'] = array_unique($this->_datas['keys']);
     }
     $this->_incQueries($_start);
     if (!ake($key, $maps)) {
         $maps[$key] = $cols;
         Utils::set('ModelsMap', $maps);
     }
     return $this;
 }