示例#1
0
 /**
  * AbstractTable constructor.
  * @param Database $db
  * @throws SchemaException
  */
 public final function __construct(Database $db)
 {
     // Bootstrap
     $this->db = $db;
     $this->dbDriver = $this->db->driver();
     $this->columns = [];
     $this->columnsKeys = [];
     $this->constraints = [];
     $this->primaryKey = null;
     // Check if necessary constants are defined
     if (!defined("static::SCHEMA_TABLE")) {
         throw SchemaException::tableInitConstant("SCHEMA_TABLE");
     } elseif (!defined("static::SCHEMA_MODEL")) {
         throw SchemaException::tableInitConstant("SCHEMA_MODEL");
     }
     // Set table's name
     $this->tableId = get_called_class();
     $this->tableName = constant("static::SCHEMA_TABLE");
     $this->tableEngine = "InnoDB";
     // Check if SCHEMA_MODEL is not NULL and it is an existing class
     $fluentModel = constant("static::SCHEMA_MODEL");
     if (is_string($fluentModel)) {
         if (!class_exists($fluentModel)) {
             throw SchemaException::badModel($this->tableId, $fluentModel);
         }
     }
     // Check if createTable method exists
     if (method_exists($this, "createTable")) {
         // Call createTable method
         call_user_func([$this, "createTable"]);
     }
     // Save reference with table name in Schema
     // Schema::getTable() method can be called with table names instead of class names however this is not
     // recommended as it can cause conflicts in project using multiple databases
     Schema::createTable($this->tableName, $this);
 }