* database connection object
* Connect to the database
$this->pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
* Return the pdo connection
* Changes a camelCase table or field name to lowercase,
return strtolower(preg_replace('/([a-z])([A-Z])/', '$1_$2', $string));
* Returns the ID of the last inserted row or sequence value
return $this->pdo->lastInsertId($param);
* handler for dynamic CRUD methods
if (! preg_match('/^(get|update|insert|delete)(.*)$/', $function, $matches)) {
throw new \BadMethodCallException($function.' is an invalid method Call');
if ('insert' == $matches[1]) {
if (! is_array($params[0]) || count($params[0]) < 1) {
throw new \InvalidArgumentException('insert values must be an array');
return $this->insert($this->camelCaseToUnderscore($matches[2]), $params[0]);
list($tableName, $fieldName) = explode('By', $matches[2], 2);
if (! isset($tableName, $fieldName)) {
throw new \BadMethodCallException($function.' is an invalid method Call');
if ('update' == $matches[1]) {
if (! is_array($params[1]) || count($params[1]) < 1) {
throw new \InvalidArgumentException('update fields must be an array');
array($this->camelCaseToUnderscore($fieldName) => $params[0])
//select and delete method
return $this->{$matches[1]}(
$this->camelCaseToUnderscore($tableName),
array($this->camelCaseToUnderscore($fieldName) => $params[0])
* Record retrieval method
* @param array $where (key is field name)
* @return array|bool (associative array for single records, multidim array for multiple records)
- public function get($tableName, $whereAnd = array(), $whereOr = array(), $whereLike = array())
+ public function get($tableName, $whereAnd = array(), $whereOr = array(), $whereLike = array())
+ foreach($whereAnd as $key => $val)
+ $cond .= " And ".$key." = :a".$s;
+ $params['a'.$s] = $val;
+ foreach($whereOr as $key => $val)
- foreach($whereAnd as $key => $val)
- $cond .= " And ".$key." = :a".$s;
- $params['a'.$s] = $val;
- foreach($whereOr as $key => $val)
- $cond .= " OR ".$key." = :a".$s;
- $params['a'.$s] = $val;
- foreach($whereLike as $key => $val)
- $cond .= " OR ".$key." like '% :a".$s."%'";
- $params['a'.$s] = $val;
- $stmt = $this->pdo->prepare("SELECT $tableName.* FROM $tableName WHERE 1 ".$cond);
+ $cond .= " OR ".$key." = :a".$s;
+ $params['a'.$s] = $val;
+ foreach($whereLike as $key => $val)
+ $cond .= " OR ".$key." like '% :a".$s."%'";
+ $params['a'.$s] = $val;
+ $stmt = $this->pdo->prepare("SELECT $tableName.* FROM $tableName WHERE 1 ".$cond);
$res = $stmt->fetchAll();
- if (! $res || count($res) != 1) {
+ if (! $res || count($res) != 1) {
} catch (\PDOException $e) {
throw new \RuntimeException("[".$e->getCode()."] : ". $e->getMessage());
- public function getAllRecords($tableName, $fields='*', $cond='', $orderBy='', $limit='')
- //echo "SELECT $tableName.$fields FROM $tableName WHERE 1 ".$cond." ".$orderBy." ".$limit;
- //print "<br>SELECT $fields FROM $tableName WHERE 1 ".$cond." ".$orderBy." ".$limit;
- $stmt = $this->pdo->prepare("SELECT $fields FROM $tableName WHERE 1 ".$cond." ".$orderBy." ".$limit);
- //print "SELECT $fields FROM $tableName WHERE 1 ".$cond." ".$orderBy." " ;
- $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
- public function getRecFrmQry($query)
- $stmt = $this->pdo->prepare($query);
- $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
- public function getRecFrmQryStr($query)
- $stmt = $this->pdo->prepare($query);
- public function getQueryCount($tableName, $field, $cond='')
- $stmt = $this->pdo->prepare("SELECT count($field) as total FROM $tableName WHERE 1 ".$cond);
+ public function getAllRecords($tableName, $fields='*', $cond='', $orderBy='', $limit='')
+ //echo "SELECT $tableName.$fields FROM $tableName WHERE 1 ".$cond." ".$orderBy." ".$limit;
+ //print "<br>SELECT $fields FROM $tableName WHERE 1 ".$cond." ".$orderBy." ".$limit;
+ $stmt = $this->pdo->prepare("SELECT $fields FROM $tableName WHERE 1 ".$cond." ".$orderBy." ".$limit);
+ //print "SELECT $fields FROM $tableName WHERE 1 ".$cond." ".$orderBy." " ;
+ $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
+ public function getRecFrmQry($query)
+ $stmt = $this->pdo->prepare($query);
+ $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
+ public function getRecFrmQryStr($query)
+ $stmt = $this->pdo->prepare($query);
+ public function getQueryCount($tableName, $field, $cond='')
+ $stmt = $this->pdo->prepare("SELECT count($field) as total FROM $tableName WHERE 1 ".$cond);
$res = $stmt->fetchAll(PDO::FETCH_ASSOC);
- if (! $res || count($res) != 1) {
+ if (! $res || count($res) != 1) {
} catch (\PDOException $e) {
throw new \RuntimeException("[".$e->getCode()."] : ". $e->getMessage());
$stmt = $this->pdo->prepare(
"UPDATE $tableName SET ". implode(',', $arrSet).' WHERE '. key($where). '=:'. key($where) . 'Field'
foreach ($set as $field => $value) {
$stmt->bindValue(':'.$field, $value);
$stmt->bindValue(':'.key($where) . 'Field', current($where));
return $stmt->rowCount();
} catch (\PDOException $e) {
throw new \RuntimeException("[".$e->getCode()."] : ". $e->getMessage());
$stmt = $this->pdo->prepare("DELETE FROM $tableName WHERE ".key($where) . ' = ?');
$stmt->execute(array(current($where)));
return $stmt->rowCount();
} catch (\PDOException $e) {
throw new \RuntimeException("[".$e->getCode()."] : ". $e->getMessage());
- public function delete2($query)
+ public function deleteQry($query)
- $stmt = $this->pdo->prepare($query);
+ $stmt = $this->pdo->prepare($query);
public function insert($tableName, array $data)
- $stmt = $this->pdo->prepare("INSERT INTO $tableName (".implode(',', array_keys($data)).")
+ $stmt = $this->pdo->prepare("INSERT INTO $tableName (".implode(',', array_keys($data)).")
VALUES (".implode(',', array_fill(0, count($data), '?')).")"
throw new \RuntimeException("[".$e->getCode()."] : ". $e->getMessage());
- public function arprint($array){
+ public function arprint($array){
+ * Maker Model Name Method
+ public function getModelMake($makeID,$nameID){
+ $vehMakeData = self::getRecFrmQry('SELECT veh_make_id,veh_make_name FROM tb_vehicle_make WHERE veh_make_id="'.$makeID.'"');
+ $vehNameData = self::getRecFrmQry('SELECT veh_name_id,veh_name FROM tb_vehicle_name WHERE veh_name_id="'.$nameID.'"');
+ return $vehMakeData[0]['veh_make_name'].' '.$vehNameData[0]['veh_name'];
* @param Int Time default 0 set
- public function getCache($sql,$cache_min=0) {
- $f = HOME_PATH.'aj_cache/'.md5($sql);
- if ( $cache_min!=0 && file_exists($f) && ( (time()-filemtime($f))/60 < $cache_min ) ) {
- $arr = unserialize(file_get_contents($f));
- $arr = self::getRecFrmQry($sql);
- fwrite($fp,serialize($arr));
+ public function getCache($sql,$cache_min=0) {
+ $f = 'cache/'.md5($sql);
+ if ( $cache_min!=0 and file_exists($f) and ( (time()-filemtime($f))/60 < $cache_min ) ) {
+ $arr = unserialize(file_get_contents($f));
+ $arr = self::getRecFrmQry($sql);
+ fwrite($fp,serialize($arr));