Exemple #1
0
 /**
  * 
  * @return PDO
  */
 public function getPDO()
 {
     return DBConnector::getDb($this->_dsn);
     //return pdo object associated with
     //the current dsn
 }
Exemple #2
0
 /**
  * 
  * {@inheritDoc}
  */
 public function updateMatchingDbTableRows(array $col_names_n_vals_2_save = array(), array $col_names_n_vals_2_match = array())
 {
     $result = null;
     if (!empty($col_names_n_vals_2_save) && count($col_names_n_vals_2_save) > 0) {
         $table_cols = $this->getTableColNames();
         $last_updtd_colname = $this->_updated_timestamp_column_name;
         if (!empty($last_updtd_colname) && in_array($last_updtd_colname, $table_cols)) {
             //set last updated timestamp to now
             $col_names_n_vals_2_save[$last_updtd_colname] = date('Y-m-d H:i:s');
         }
         $pkey_col_name = $this->getPrimaryColName();
         if (array_key_exists($pkey_col_name, $col_names_n_vals_2_save)) {
             //don't update the primary key
             unset($col_names_n_vals_2_save[$pkey_col_name]);
         }
         // remove non-existent table columns from the data
         // and check that existent table columns have values of
         // the right data type: ie. Boolean, NULL, Number or String.
         foreach ($col_names_n_vals_2_save as $key => $val) {
             if (!in_array($key, $table_cols)) {
                 unset($col_names_n_vals_2_save[$key]);
             } else {
                 if (!is_bool($val) && !is_null($val) && !is_numeric($val) && !is_string($val)) {
                     $msg = "ERROR: the value " . PHP_EOL . var_export($val, true) . PHP_EOL . " you are trying to update {$this->_table_name}." . "{$key} with is not acceptable ('" . gettype($val) . "'" . " supplied). Boolean, NULL, numeric or string value expected." . PHP_EOL . "Data supplied to " . get_class($this) . '::' . __FUNCTION__ . '(...).' . " for update:" . PHP_EOL . var_export($col_names_n_vals_2_save, true) . PHP_EOL . PHP_EOL;
                     throw new \GDAO\ModelInvalidUpdateValueSuppliedException($msg);
                 }
             }
         }
         //update statement
         $update_qry_obj = (new QueryFactory($this->_pdo_driver_name))->newUpdate();
         $update_qry_obj->table($this->_table_name);
         $update_qry_obj->cols($col_names_n_vals_2_save);
         if (!empty($col_names_n_vals_2_match) && count($col_names_n_vals_2_match) > 0) {
             foreach ($col_names_n_vals_2_match as $colname => $colval) {
                 if (is_array($colval)) {
                     //quote all string values
                     array_walk($colval, function (&$val, $key, $pdo) {
                         $val = is_string($val) ? $pdo->quote($val) : $val;
                     }, $this->getPDO());
                     $update_qry_obj->where("{$colname} IN (" . implode(',', $colval) . ") ");
                 } else {
                     //NOTE: not pdo quoting $colval here because when
                     //DBConnector::executeQuery($updt_qry, $updt_qry_params)
                     //is called, the pdo quoting gets handled by DBConnector.
                     $update_qry_obj->where("{$colname} = ?", $colval);
                 }
             }
         }
         $updt_qry = $update_qry_obj->__toString();
         $updt_qry_params = $update_qry_obj->getBindValues();
         $result = $this->_db_connector->executeQuery($updt_qry, $updt_qry_params, true);
         if ($result[0] === true) {
             //return number of affected rows
             $pdo_statement_used_for_query = $result[1];
             $result = $pdo_statement_used_for_query->rowCount();
         } else {
             //return boolean result of the \PDOStatement::execute() call
             //from $this->_db_connector->executeQuery($updt_qry, $updt_qry_params, true);
             $result = $result[0];
         }
     }
     return $result;
 }