few more changes to the CRUD layer in ams

This commit is contained in:
shubham_meena 2014-05-27 14:19:37 +05:30
parent 33eece53b4
commit 333cfcc8d2

View file

@ -73,28 +73,68 @@ class DBLayer{
* @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));
$field_options = implode(',', array_keys($data));
try{
$sth = $this->PDO->prepare("INSERT INTO $tb_name ($field_options) VALUE ($field_values)");
foreach ($data as $key => $value )
{
$sth->bindValue(":$key", $value);
}
$this->PDO->beginTransaction(); $this->PDO->beginTransaction();
$statement->execute($params); //execution
$sth->execute();
$lastId =$this->PDO->lastInsertId(); $lastId =$this->PDO->lastInsertId();
$this->PDO->commit(); $this->PDO->commit();
}catch (Exception $e)
{
//for rolling back the changes during transaction
$this->PDO->rollBack();
throw new Exception("error in inseting");
}
return $lastId; 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(); $this->PDO->beginTransaction();
$sth->execute(array($data)); $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();
$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();