few more changes to the CRUD layer in ams
This commit is contained in:
parent
c65a2001ab
commit
684ae3bdc7
1 changed files with 54 additions and 15 deletions
|
@ -67,34 +67,74 @@ class DBLayer{
|
||||||
return $statement;
|
return $statement;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* execute a query (an insertion query) that has parameters and return the id of it's insertion
|
* execute a query (an insertion query) that has parameters and return the id of it's insertion
|
||||||
* @param $query the mysql query
|
* @param $query the mysql query
|
||||||
* @param $params the parameters that are being used by the query
|
* @param $params the parameters that are being used by the query
|
||||||
* @return returns the id of the last inserted element.
|
* @return returns the id of the last inserted element.
|
||||||
*/
|
*/
|
||||||
public function executeReturnId($query,$params){
|
public function executeReturnId($tb_name,$data){
|
||||||
$statement = $this->PDO->prepare($query);
|
$field_values =':'. implode(',:', array_keys($data));
|
||||||
$this->PDO->beginTransaction();
|
$field_options = implode(',', array_keys($data));
|
||||||
$statement->execute($params);
|
try{
|
||||||
$lastId =$this->PDO->lastInsertId();
|
$sth = $this->PDO->prepare("INSERT INTO $tb_name ($field_options) VALUE ($field_values)");
|
||||||
$this->PDO->commit();
|
foreach ($data as $key => $value )
|
||||||
return $lastId;
|
{
|
||||||
|
$sth->bindValue(":$key", $value);
|
||||||
|
}
|
||||||
|
$this->PDO->beginTransaction();
|
||||||
|
//execution
|
||||||
|
$sth->execute();
|
||||||
|
$lastId =$this->PDO->lastInsertId();
|
||||||
|
$this->PDO->commit();
|
||||||
|
}catch (Exception $e)
|
||||||
|
{
|
||||||
|
//for rolling back the changes during transaction
|
||||||
|
$this->PDO->rollBack();
|
||||||
|
throw new Exception("error in inseting");
|
||||||
|
}
|
||||||
|
return $lastId;
|
||||||
}
|
}
|
||||||
/**
|
|
||||||
|
|
||||||
|
/**
|
||||||
*
|
*
|
||||||
* Select function using prepared statement
|
* Select function using prepared statement
|
||||||
* @param string $tb_name Table Name to Select
|
* @param string $tb_name Table Name to Select
|
||||||
* @param array $data Associative array
|
* @param array $data Associative array
|
||||||
* @param string $where where to select
|
* @param string $where where to select
|
||||||
* @return array Array containing fetched data
|
* @return statement object
|
||||||
*/
|
*/
|
||||||
public function select($query, $data)
|
public function selectWithParameter($param, $tb_name, $data, $where)
|
||||||
{
|
{
|
||||||
try{
|
try{
|
||||||
$sth = $this->PDO->prepare($query);
|
$sth = $this->PDO->prepare("SELECT $param FROM $tb_name WHERE $where");
|
||||||
|
$this->PDO->beginTransaction();
|
||||||
|
$sth->execute($data);
|
||||||
|
$this->PDO->commit();
|
||||||
|
}catch(Exception $e)
|
||||||
|
{
|
||||||
|
$this->PDO->rollBack();
|
||||||
|
throw new Exception("error selection");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return $sth;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* Select function using prepared statement
|
||||||
|
* @param string $tb_name Table Name to Select
|
||||||
|
* @param array $data Associative array
|
||||||
|
* @param string $where where to select
|
||||||
|
* @return statement object
|
||||||
|
*/
|
||||||
|
public function select($tb_name, $data ,$where)
|
||||||
|
{
|
||||||
|
try{
|
||||||
|
$sth = $this->PDO->prepare("SELECT * FROM $tb_name WHERE $where");
|
||||||
$this->PDO->beginTransaction();
|
$this->PDO->beginTransaction();
|
||||||
$sth->execute(array($data));
|
$sth->execute($data);
|
||||||
$this->PDO->commit();
|
$this->PDO->commit();
|
||||||
}catch(Exception $e)
|
}catch(Exception $e)
|
||||||
{
|
{
|
||||||
|
@ -152,7 +192,6 @@ class DBLayer{
|
||||||
$sth = $this->PDO->prepare("INSERT INTO $tb_name ($field_options) VALUE ($field_values)");
|
$sth = $this->PDO->prepare("INSERT INTO $tb_name ($field_options) VALUE ($field_values)");
|
||||||
foreach ($data as $key => $value )
|
foreach ($data as $key => $value )
|
||||||
{
|
{
|
||||||
|
|
||||||
$sth->bindValue(":$key", $value);
|
$sth->bindValue(":$key", $value);
|
||||||
}
|
}
|
||||||
$this->PDO->beginTransaction();
|
$this->PDO->beginTransaction();
|
||||||
|
|
Loading…
Reference in a new issue