Nom de la table à selectionner * @driver [optional] DatabaseDriver label * * @return this Retourne une instance de l'ORM * */ public static function get($table_name, $driver=null){ /* [0] Initialisation des attributs =========================================================*/ $schema = [ 'database' => DatabaseDriver::get($driver)->getConfig()['dbname'], 'table' => null, 'columns' => null ]; /* [1] On vérifie que la table existe =========================================================*/ /* (1) Requête */ $checkTable = DatabaseDriver::getPDO($driver)->query("SHOW tables FROM ".$schema['database']); $checkTableResult = DatabaseDriver::delNumeric( $checkTable->fetchAll() ); /* (2) On met en forme les données */ $tables = []; foreach($checkTableResult as $table) $tables[] = $table['Tables_in_'.$schema['database']]; /* (3) Si n'existe pas, on renvoie une erreur */ if( !in_array($table_name, $tables) ) throw new \Exception('[*] Unknown table.'); /* (4) On enregistre les données */ $schema['table'] = $table_name; /* [2] Si la table existe, on récupère les colonnes =========================================================*/ /* (1) On récupère les colonnes */ $getColumns = DatabaseDriver::getPDO($driver)->query("SHOW columns FROM ".$schema['database'].'.'.$table_name); $columnsResult = DatabaseDriver::delNumeric( $getColumns->fetchAll() ); /* (2) On met en forme les données */ $columns = []; foreach($columnsResult as $col){ // On formatte le type // $type = $col['Type']; if( preg_match('/^(int|float|varchar|text)/i', $type, $m) ) $type = strtolower($m[1]); // On ajoute la colonne // $columns[$col['Field']] = [ 'type' => $type, 'primary' => $col['Key'] == 'PRI' ]; } /* (3) Si on trouve rien, on envoie une erreur */ if( !is_array($columns) || count($columns) == 0 ) throw new \Exception('[*] Cannot fetch columns'); /* (4) On enregistre les colonnes */ $schema['columns'] = $columns; /* [3] On récupère les clés étrangères =========================================================*/ /* (1) On récupère le texte du 'CREATE TABLE' */ $getCreateTable = DatabaseDriver::getPDO($driver)->query("show create table ".$table_name); if( is_bool($getCreateTable) ) throw new \Exception('[*] Cannot fetch constrains'); $create_table = $getCreateTable->fetch()['Create Table']; /* (2) On découpte en lignes */ $create_table_lines = explode("\n", $create_table); /* (3) Pour chaque ligne, si c'est une contrainte, on l'enregistre dans la colonne associée */ foreach($create_table_lines as $i=>$line) if( preg_match('/CONSTRAINT `.+` FOREIGN KEY \(`(.+)`\) REFERENCES `(.+)` \(`(.+)`\)+/i', $line, $m) ) $schema['columns'][$m[1]]['references'] = [$m[2], $m[3]]; /* [3] On renvoie une instance de 'Rows' =========================================================*/ return new Rows($schema, $driver); } }; // // USE CASE :: ACCESS TABLE // ORM::Table = ORM::Table('user'); // // // USE CASE :: getBy{ATTRIBUTE} // ORM::Row = ORM::Table->getByUsername('someUsername'); // ORM_FETCH by default // ORM::Row = ORM::Table->getByUsername('someUsername', ORM_FETCH); // ORM::Column = ORM::Table->getByUsername('someUsername', ORM_FETCHALL); // // // USE CASE :: getById -> primary key(s) // ORM::Row = ORM::Table->getById(5, 7); // because PRIMARY KEY is composed by '5' and '7' // // // USE CASE :: getAll // ORM::Column = ORM::Table->getAll(); // // // USE CASE :: select(FIELD) // mixed = ORM::Row->select('username'); // // // USE CASE :: select(FIELD1, FIELD2, ...) // mixed = ORM::Row->select('id_user', 'username');