Example #1
0
 /**
  * 
  * Save the specified or already existing data for this record to the db.
  * 
  * This save operation is gaurded by the PDO transaction mechanism. 
  * If the save operation fails all changes are rolled back.
  * 
  * If there is no transaction mechanism available a 
  * \LeanOrm\Model\RecordOperationNotSupportedByDriverException Exception is 
  * thrown.
  * 
  * @param \GDAO\Model\RecordInterface|array $data_2_save
  * 
  * @throws \LeanOrm\Model\RecordOperationNotSupportedByDriverException
  * 
  * @return bool true for a successful save, false for failed save, null: no changed data to save
  * 
  */
 public function saveInTransaction($data_2_save = null)
 {
     $pdo_obj = $this->_model->getPDO();
     if ($pdo_obj instanceof \PDO) {
         // start the transaction
         $pdo_obj->beginTransaction();
         try {
             $save_status = $this->save($data_2_save);
             // attempt the save
             if ($save_status === true) {
                 // entire save was valid, keep it
                 $pdo_obj->commit();
                 return true;
             } else {
                 if ($save_status === false) {
                     // at least one part of the save was *not* valid.
                     // throw it all away.
                     $pdo_obj->rollBack();
                     return false;
                 } else {
                     return null;
                     //$save_status === null nothing was done
                 }
             }
         } catch (\Exception $e) {
             // roll back and throw the exception
             $pdo_obj->rollBack();
             throw $e;
         }
     } else {
         $msg = get_class($this) . ' Does Not Support ' . __FUNCTION__ . '(...)';
         throw new RecordOperationNotSupportedByDriverException($msg);
     }
 }
Example #2
0
 /**
  * 
  * {@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);
         }
     }
 }
 public function __construct($dsn = '', $uname = '', $pswd = '', $pdo_drv_opts = [], $ext_opts = [])
 {
     if ($dsn || $uname || $pswd || $pdo_drv_opts || $ext_opts) {
         parent::__construct($dsn, $uname, $pswd, $pdo_drv_opts, $ext_opts);
     }
 }