0
I need to input data into 3 tables related using the PDO. The first table t_repo_proj (repositórios)
, and the second t_grnte_proj (gerentes)
.
I need to know which manager is associated with which repository. To do this, I created a 3° table a t_repo_proj_grnte
that will receive only the SK (not incremental) from the previous 2 tables.
Follow my class of connection:
require_once ('C:\xampp\htdocs\PhpProject1\Classes\Config.php');
class DB{
private static $instance;
public static function getInstance(){
if(!isset(self::$instance)){
try {
self::$instance = new PDO('mysql:host=' . DB_HOST . ';dbname=' . DB_NAME, DB_USER, DB_PASS);
self::$instance->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
self::$instance->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
} catch (PDOException $e) {
echo $e->getMessage();
}
}
return self::$instance;
}
public static function prepare($sql){
return self::getInstance()->prepare($sql);
}
}
I have a crud class that inherits from DB, but it makes my select and delete only....
My class Managers:
require_once ('C:\xampp\htdocs\PhpProject1\Classes\Crud.php');
class Gerentes extends Crud{
protected $table = 't_grnte_proj';
private $nm_grnte_proj;
private $sk_grnte_proj;
public function setNm_grnte_proj($nm_grnte_proj) {
$this->nm_grnte_proj = $nm_grnte_proj;
}
public function getNm_grnte_proj() {
return $this->nm_grnte_proj;
}
public function setSk_grnte_proj($sk_grnte_proj) {
$this->sk_grnte_proj = $sk_grnte_proj;
}
public function getSk_grnte_proj() {
return $this->sk_grnte_proj;
}
public function insert(){
$sql = "insert into $this->table (sk_grnte_proj,nm_grnte_proj) values (:sk_grnte_proj,:nm_grnte_proj)";
$stmt = DB::prepare($sql);
$stmt->bindParam(':sk_grnte_proj', $this->sk_grnte_proj);
$stmt->bindParam(':nm_grnte_proj', $this->nm_grnte_proj);
return $stmt->execute();
}
}
My repositories class:
require_once ('C:\xampp\htdocs\PhpProject1\Classes\Crud.php');
class Repositorios extends Crud{
protected $table = 't_repo_proj';
private $ds_repo_proj;
private $sk_repo_proj;
public function setDs_repo_proj($ds_repo_proj) {
$this->ds_repo_proj = $ds_repo_proj;
}
public function getDs_repo() {
return $this->ds_repo_proj;
}
public function setSk_repo_proj($ds_repo_proj) {
$this->sk_repo_proj = $ds_repo_proj;
}
public function getSk_repo_proj() {
return $this->sk_repo_proj;
}
public function insert(){
$sql = "insert into $this->table (sk_repo_proj,ds_repo_proj) values (:sk_repo_proj,:ds_repo_proj)";
$stmt = DB::prepare($sql);
$stmt->bindParam(':sk_repo_proj', $this->sk_repo_proj);
$stmt->bindParam(':ds_repo_proj', $this->ds_repo_proj);
return $stmt->execute();
}
}
And finally, a part of my index where I introduce the classes and call the insert method.
if(isset($_POST['cadastrar'])):
$cargo=$_POST['cargo'];
if($cargo=='desenvolvedor'){
$sk_desenv = $_POST['txt_codigo_pessoal'];
$nm_desenv = $_POST['txt_nome'];
$sk_repo_proj = $_POST['txt_sk_repo_proj'];
$ds_repo_proj = $_POST['txt_repo'];
$desenvolvedor->setNm_desenv($nm_desenv);
$desenvolvedor->setSk_desenv($sk_desenv);
$repositorio->setSk_repo_proj($sk_repo_proj);
$repositorio->setDs_repo_proj($ds_repo_proj);
$repositorio->insert();
$desenvolvedor->insert();
}
if($cargo=='gerente') {
$sk_grnte_proj = $_POST['txt_codigo_pessoal'];
$nm_grnte_proj = $_POST['txt_nome'];
$sk_repo_proj = $_POST['txt_sk_repo_proj'];
$ds_repo_proj = $_POST['txt_repo'];
$gerente->setNm_grnte_proj($nm_grnte_proj);
$gerente->setSk_grnte_proj($sk_grnte_proj);
$repositorio->setSk_repo_proj($sk_repo_proj);
$repositorio->setDs_repo_proj($ds_repo_proj);
$repositorio->insert();
$gerente->insert();
}
endif;
?>
So, the functions Insert (repository) and Insert (manager) would be executed in a single function in another class for example? could you give me an example? I can make a transaction from my getInstance() of the connection class?
– Gabriel Silva
Exactly that
– gabrielfalieri