コード例 #1
0
ファイル: Model.php プロジェクト: rotexsoft/leanorm
 /**
  * 
  * {@inheritDoc}
  */
 public function __construct($dsn = '', $username = '', $passwd = '', array $pdo_driver_opts = array(), array $extra_opts = array())
 {
     $pri_col_not_set_exception = null;
     try {
         parent::__construct($dsn, $username, $passwd, $pdo_driver_opts, $extra_opts);
     } catch (\GDAO\ModelPrimaryColNameNotSetDuringConstructionException $e) {
         //$this->_primary_col (primary key colun has not yet been set)
         //hold this exception for later if necessary
         $pri_col_not_set_exception = $e;
     }
     DBConnector::configure($dsn, null, $dsn);
     //use $dsn as connection name in 3rd parameter
     DBConnector::configure('username', $username, $dsn);
     //use $dsn as connection name in 3rd parameter
     DBConnector::configure('password', $passwd, $dsn);
     //use $dsn as connection name in 3rd parameter
     if (count($pdo_driver_opts) > 0) {
         DBConnector::configure('driver_options', $pdo_driver_opts, $dsn);
         //use $dsn as connection name in 3rd parameter
     }
     foreach ($extra_opts as $e_opt_key => $e_opt_val) {
         if (is_string($e_opt_key) && in_array($e_opt_key, static::$_valid_extra_opts_keys_4_dbconnector)) {
             DBConnector::configure($e_opt_key, $e_opt_val, $dsn);
             //use $dsn as connection name in 3rd parameter
         }
     }
     $this->_db_connector = DBConnector::create($dsn);
     //use $dsn as connection name
     $this->_pdo_driver_name = $this->getPDO()->getAttribute(\PDO::ATTR_DRIVER_NAME);
     ////////////////////////////////////////////////////////
     //Get and Set Table Schema Meta Data if Not Already Set
     ////////////////////////////////////////////////////////
     if (empty($this->_table_cols) || count($this->_table_cols) <= 0) {
         // a column definition factory
         $column_factory = new ColumnFactory();
         $schema_class_name = '\\Aura\\SqlSchema\\' . ucfirst($this->_pdo_driver_name) . 'Schema';
         // the schema discovery object
         $schema = new $schema_class_name($this->getPDO(), $column_factory);
         $this->_table_cols = array();
         $schema_definitions = $schema->fetchTableCols($this->_table_name);
         foreach ($schema_definitions as $colname => $metadata_obj) {
             $this->_table_cols[$colname] = array();
             $this->_table_cols[$colname]['name'] = $metadata_obj->name;
             $this->_table_cols[$colname]['type'] = $metadata_obj->type;
             $this->_table_cols[$colname]['size'] = $metadata_obj->size;
             $this->_table_cols[$colname]['scale'] = $metadata_obj->scale;
             $this->_table_cols[$colname]['notnull'] = $metadata_obj->notnull;
             $this->_table_cols[$colname]['default'] = $metadata_obj->default;
             $this->_table_cols[$colname]['autoinc'] = $metadata_obj->autoinc;
             $this->_table_cols[$colname]['primary'] = $metadata_obj->primary;
             if (is_null($this->_primary_col) && $metadata_obj->primary) {
                 //this is a primary column
                 $this->_primary_col = $metadata_obj->name;
             }
         }
     }
     //if $this->_primary_col is still null at this point, throw an exception.
     if (is_null($this->_primary_col)) {
         throw $pri_col_not_set_exception;
     }
     $table_cols = $this->getTableColNames();
     foreach ($this->_relations as $relation_name => $relation_info) {
         if (in_array($relation_name, $table_cols)) {
             //Error trying to add a relation whose name collides with an actual
             //name of a column in the db table associated with this model.
             $msg = "ERROR: You cannont add a relationship with the name '{$relation_name}' " . " to the Model (" . get_class($this) . "). The database table " . " '{$this->getTableName()}' associated with the " . " model (" . get_class($this) . ") already contains" . " a column with the same name." . PHP_EOL . get_class($this) . '::' . __FUNCTION__ . '(...).' . PHP_EOL;
             throw new \GDAO\Model\RecordRelationWithSameNameAsAnExistingDBTableColumnNameException($msg);
         }
     }
 }