Update query builder
Auteur: marten - 09 december 2009 - 16:41 - Gekeurd door: Koen - Hits: 3705 - Aantal punten: 4.00 (1 stem)
Update query builder
Universele functie om via een array een tabel te updaten. Onmisbaar in een database class.
Voordelen:
* Niet voor ieder onderdeel een aparte modify functie.
* Controle of het veld uit de array bestaat.
* Toepasbaar op allerlei databases icm PDO
Voorbeeld gebruik
$this->pdo = $db; // Natuurlijk moet er een db connectie zijn. Ik gebruik hier PDO
$aArray['veld1'] = 'Waarde1';
$aArray['veld2'] = 'Waarde2';
$aArray['veld3'] = 'Waarde3';
$aArray['veld4'] = 'Waarde4';
$oFoo->modify('table', 'idVeld', 12, $aArray);
$this -> pdo = $db ; // Natuurlijk moet er een db connectie zijn. Ik gebruik hier PDO
$aArray [ 'veld1' ] = 'Waarde1' ;
$aArray [ 'veld2' ] = 'Waarde2' ;
$aArray [ 'veld3' ] = 'Waarde3' ;
$aArray [ 'veld4' ] = 'Waarde4' ;
$oFoo -> modify ( 'table' , 'idVeld' , 12 , $aArray ) ;
Code:
/**
* Update query builder
*
* @author Marten van Urk .: ComSi www.comsi.nl :.
* @package Invoice
* @subpackage Products
* @param String $table Table which will be updated
* @param String $idField The updated key field (for example the auto increment field) Will be used in combination with $id for the where statement
* @param Integer $id The id of the given field
* @param Array $aUpdateValues
* @since 1.0
* @return Boolean On succes the boolean true will be returned otherwise, on a fail the boolean false will be returned.
*/
static private $tableLoaded = array();
public function modify($table, $idField, $id, $aUpdateValues = array()) {
if(count($aUpdateValues ) == 0) {
return false;
}
try {
/**
* Check if query is runned before
**/
if(!isset($this->tableLoaded[$table])) {
$this->tableLoaded[$table] = $this->db->query("SHOW COLUMNS FROM " . $table);
}
$aFields = array();
foreach ($this->tableLoaded[$table] as $r) {
$aFields[$r[0]] = '';
}
} catch (Exception $e) {
return false;
}
foreach ($aUpdateValues as $key => $value) {
if(array_key_exists($key, $aFields)) {
if(ctype_digit($value)) {
$sets[] = '`'.$key.'` = ' .$value;
} else {
$sets[] = '`'.$key.'` = \''.$value.'\'';
}
}
}
$sQuery = "UPDATE `" .$table. "` " . 'SET ' .implode(', ', $sets) . " WHERE " .$idField. " = " . $id;
try {
$rResult_update = $this->db->query($sQuery);
return true;
} catch (Exception $e) {
return false;
}
}
/**
* Update query builder
*
* @author Marten van Urk .: ComSi www.comsi.nl :.
* @package Invoice
* @subpackage Products
* @param String $table Table which will be updated
* @param String $idField The updated key field (for example the auto increment field) Will be used in combination with $id for the where statement
* @param Integer $id The id of the given field
* @param Array $aUpdateValues
* @since 1.0
* @return Boolean On succes the boolean true will be returned otherwise, on a fail the boolean false will be returned.
*/
public function modify
( $table , $idField , $id , $aUpdateValues = array ( ) ) { if ( count ( $aUpdateValues ) == 0 ) { return false ;
}
try {
/**
* Check if query is runned before
**/
if ( ! isset ( $this -> tableLoaded [ $table ] ) ) { $this -> tableLoaded [ $table ] = $this -> db -> query ( "SHOW COLUMNS FROM " . $table ) ;
}
foreach ( $this -> tableLoaded [ $table ] as $r ) {
$aFields [ $r [ 0 ] ] = '' ;
}
} catch ( Exception $e ) {
return false ;
}
foreach ( $aUpdateValues as $key => $value ) {
$sets [ ] = '`' . $key . '` = ' . $value ;
} else {
$sets [ ] = '`' . $key . '` = \' '.$value.' \'' ;
}
}
}
$sQuery = "UPDATE `" . $table . "` " . 'SET ' . implode ( ', ' , $sets ) . " WHERE " . $idField . " = " . $id ;
try {
$rResult_update = $this -> db -> query ( $sQuery ) ;
return true ;
} catch ( Exception $e ) {
return false ;
}
}
Download code (.txt)
Stemmen
Niet ingelogd.