Archive some objects with inheritance

I have a difficult problem and I don’t know how to do it.

I want to archive some objects and then delete them from the initial tables. The problem is that these objects are connected to each other. I have code that is close to work, but I think this is not a clean way to do this. It consists of SQL queries that copy rows with their identifiers into new tables. Another problem is that I do not need fields for archiving, so the archive objects are not exactly the same as the original ones. I am using a raw SQL query and not DQL due to the size of my tables.

I want to back up these objects: Colle, ColleQC, QC, PasserColle, Reponse, ReponseQC, StatistiqueColle, StatistiqueQuestion, RepartitionColle, RepartitionQuestion, Tuteur By BanqueColle, BanqueColleQC, BanqueQC, BanquePasserColle, BanqueReponse, BanqueReponseQC, BanqueStatistiqueColle, BanqueStatistiqueQuestion, BanqueRepartitionColle, BanqueRepartitionQuestion, AncienAdherent.

I will use this archive for another part of my application.

Example table structure: enter image description here

(Tuteur and AncienAdherent extend the user)

Here is the part of the code that I did for archiving, but I don't think this is a clean way to do this:

public function archiveTuteurs() {
    $db = $this->em->getConnection();
    $query = "INSERT INTO ancien_adherent (id)
              SELECT u.id
              FROM user u
              WHERE discr = 'tuteur'";
    $stmt = $db->prepare($query);
    $stmt->execute();

    $query2 = "UPDATE user
              SET user.discr = 'ancien'
              WHERE discr = 'tuteur'";
    $stmt = $db->prepare($query2);
    $stmt->execute();

    return true;
}

public function archiveColles() {
    $db = $this->em->getConnection();
    $query = "INSERT INTO banque_colle (id, typeColle, nom, temps_epreuve, matiere_id, dateCreation, ordre, discr) 
              SELECT colle.id, colle.typeColle, colle.nom, colle.temps_epreuve, colle.matiere_id, colle.dateCreation, colle.ordre, colle.discr
              FROM colle";
    $stmt = $db->prepare($query);
    $stmt->execute();

    $query2 = "INSERT INTO banque_colle_qc (id)
               SELECT colle_qc.id
               FROM colle_qc";
    $stmt = $db->prepare($query2);
    $stmt->execute();

    return true;
}

public function archiveQC() {
    $db = $this->em->getConnection();
    $query = "INSERT INTO banque_qc (id, titre, id_colle, ordre, qcPere, enonce, donnees, item1, item2, item3, item4,
                          item5, corrige_item1, corrige_item2, corrige_item3, corrige_item4, corrige_item5, item1_vrai,
                          item2_vrai, item3_vrai, item4_vrai, item5_vrai, item1_annule, item2_annule, item3_annule,
                          item4_annule, item5_annule, multiple_choices, inclu) 
              SELECT qc.id, qc.titre, qc.id_colle, qc.ordre, qc.qcPere, qc.enonce, qc.donnees, qc.item1, qc.item2,
                     qc.item3, qc.item4, qc.item5, qc.corrige_item1, qc.corrige_item2, qc.corrige_item3, qc.corrige_item4,
                     qc.corrige_item5, qc.item1_vrai, qc.item2_vrai, qc.item3_vrai, qc.item4_vrai, qc.item5_vrai, 
                     qc.item1_annule, qc.item2_annule, qc.item3_annule, qc.item4_annule, qc.item5_annule, 
                     qc.multiple_choices, qc.inclu
              FROM qc
              ORDER BY qc.qcPere ASC";
    $stmt = $db->prepare($query);
    $stmt->execute();

    return true;
}

public function archivePassages() {
    $db = $this->em->getConnection();
    $query = "INSERT INTO banque_passer_colle (colle_id, dateDebut, note) 
              SELECT passer_colle.colle_id, passer_colle.dateDebut, passer_colle.note
              FROM passer_colle";
    $stmt = $db->prepare($query);
    $stmt->execute();

    return true;
}

public function archiveReponses() {
    $db = $this->em->getConnection();
    $query = "INSERT INTO banque_reponse (id, discr)
              SELECT reponse.id, reponse.discr
              FROM reponse
              WHERE discr='reponseQC'";
    $stmt = $db->prepare($query);
    $stmt->execute();

    $query2 = "INSERT INTO banque_reponse_qc (id, question, A, B, C, D, E, note)
              SELECT reponse_qc.id, reponse_qc.question, reponse_qc.A, reponse_qc.B, reponse_qc.C, reponse_qc.D, 
                     reponse_qc.E, reponse_qc.note
              FROM reponse_qc";
    $stmt = $db->prepare($query2);
    $stmt->execute();

    return true;
}

public function archiveStats() {
    $db = $this->em->getConnection();
    $query = "INSERT INTO banque_statistiquecolle (id, colle_id, effectif, moyenne, mediane, note100, major, minor)
              SELECT sc.id, sc.colle_id, sc.effectif, sc.moyenne, sc.mediane, sc.note100, sc.major, sc.minor
              FROM statistiquecolle_groupe scg 
              LEFT JOIN  statistiquecolle sc ON sc.id = scg.statistiquecolle_id
              WHERE scg.groupe_id = 1 
              AND sc.id NOT IN (SELECT sc1.id
                                FROM statistiquecolle_groupe scg1 
                                LEFT JOIN  statistiquecolle sc1 ON sc1.id = scg1.statistiquecolle_id
                                WHERE scg1.groupe_id != 1)";
    $stmt = $db->prepare($query);
    $stmt->execute();

    $query2 = "INSERT INTO banque_statistiquequestion (id, question_id, moyenne, nbReponseTot, nbReponseA, nbReponseB,
                                                       nbReponseC, nbReponseD, nbReponseE)
              SELECT sq.id, sq.question_id, sq.moyenne, sq.nbReponseTot, sq.nbReponseA, sq.nbReponseB, sq.nbReponseC, 
              sq.nbReponseD, sq.nbReponseE
              FROM statistiquequestion_groupe sqg 
              LEFT JOIN  statistiquequestion sq ON sq.id = sqg.statistiquequestion_id
              WHERE sqg.groupe_id = 1 
              AND sq.id NOT IN (SELECT sq1.id
                                FROM statistiquequestion_groupe sqg1 
                                LEFT JOIN  statistiquequestion sq1 ON sq1.id = sqg1.statistiquequestion_id
                                WHERE sqg1.groupe_id != 1)";
    $stmt = $db->prepare($query2);
    $stmt->execute();

    $query3 = "INSERT INTO banque_repartitioncolle (id, statColle_id, note, nombre, percentOfEffectif)
              SELECT rc.id, rc.statColle_id, rc.note, rc.nombre, rc.percentOfEffectif
              FROM repartitioncolle rc
              WHERE rc.statColle_id IN (SELECT bsc.id
                                        FROM banque_statistiquecolle bsc)";
    $stmt = $db->prepare($query3);
    $stmt->execute();

    $query4 = "INSERT INTO banque_repartitionquestion (id, statQuestion_id, note, nombre, percentOfEffectif)
              SELECT rq.id, rq.statQuestion_id, rq.note, rq.nombre, rq.percentOfEffectif
              FROM repartitionquestion rq
              WHERE rq.statQuestion_id IN (SELECT bsq.id
                                        FROM banque_statistiquequestion bsq)";
    $stmt = $db->prepare($query4);
    $stmt->execute();

    return true;
}
+4
source share
1 answer

, ( ) - SQL. ,

# Table1.field1
ALTER TABLE Table1 DROP FOREIGN KEY FK_Table1_field1;
ALTER TABLE Table1 DROP INDEX IDX_Table1_field1;

# Table1.field2
ALTER TABLE Table1 DROP FOREIGN KEY FK_Table1_field2;
ALTER TABLE Table1 DROP INDEX IDX_Table1_field2;

SQL, .

DROP TABLE IF EXISTS Table1;
DROP TABLE IF EXISTS Table2;

ALTER TABLE Table1 
    ADD INDEX IDX_Table1_field1 (field1 ASC);
ALTER TABLE Table1
    ADD CONSTRAINT FK_Table1_field1
    FOREIGN KEY (field1)
    REFERENCES OtherTable (xxxx)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION;

sql, 4 sql , , .

0

Source: https://habr.com/ru/post/1678061/


All Articles