Ubiquity  2.0.0
php rapid development framework
DbModelsCreator.php
Go to the documentation of this file.
1 <?php
3 
5 
6 
8  private $pdoObject;
9 
10  protected function init($config){
11  parent::init($config);
12  $this->connect($config["database"]);
13  }
17  private function connect($config) {
18  try {
19  $this->pdoObject = new \PDO(
20  $config["type"].':host=' . $config["serverName"] . ';dbname='
21  . $config["dbName"] . ';port:' . $config["port"],
22  $config["user"], $config["password"]);
23  $this->pdoObject->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
24  $this->pdoObject->exec("SET CHARACTER SET utf8");
25 
26  } catch (\PDOException $e) {
27  print "Error!: " . $e->getMessage() . "<br/>";
28  }
29  }
30 
31 
32  protected function getTablesName(){
33  $sql = 'SHOW TABLES';
34  $query = $this->pdoObject->query($sql);
35  return $query->fetchAll(\PDO::FETCH_COLUMN);
36  }
37 
38  protected function getFieldsInfos($tableName) {
39  $fieldsInfos=array();
40  $recordset = $this->pdoObject->query("SHOW COLUMNS FROM `{$tableName}`");
41  $fields = $recordset->fetchAll(\PDO::FETCH_ASSOC);
42  foreach ($fields as $field) {
43  $fieldsInfos[$field['Field']] = ["Type"=>$field['Type'],"Nullable"=>$field["Null"]];
44  }
45  return $fieldsInfos;
46  }
47 
48  protected function getPrimaryKeys($tableName){
49  $fieldkeys=array();
50  $recordset = $this->pdoObject->query("SHOW KEYS FROM `{$tableName}` WHERE Key_name = 'PRIMARY'");
51  $keys = $recordset->fetchAll(\PDO::FETCH_ASSOC);
52  foreach ($keys as $key) {
53  $fieldkeys[] = $key['Column_name'];
54  }
55  return $fieldkeys;
56  }
57 
58  protected function getForeignKeys($tableName,$pkName){
59  $recordset = $this->pdoObject->query("SELECT *
60  FROM
61  information_schema.KEY_COLUMN_USAGE
62  WHERE
63  REFERENCED_TABLE_NAME = '".$tableName."'
64  AND REFERENCED_COLUMN_NAME = '".$pkName."'
65  AND TABLE_SCHEMA = '".$this->config["dbName"]."';");
66  return $recordset->fetchAll(\PDO::FETCH_ASSOC);
67  }
68 }
connect($config)
Réalise la connexion à la base de données.