Write data in 3 PDO-related tables

0

I need to insert data into related 3 tables using 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. For this, I created a 3rd table with t_repo_proj_grnte that will receive only the SK (not incremental) of the 2 previous tables.

Here is my connection class:

   

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 raw class that inherits from DB, but it does 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 class repositories:



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 instantiate 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;  

        ?>

    
asked by anonymous 28.03.2018 / 14:47

1 answer

0

You're going to have to turn all this into a single function, use beginTransaction (), rollback () in php, all this within a try catch

    
28.03.2018 / 15:02