<?php

namespace Entity\Model;

use Helpers\QueryBuilder\QueryBuilder;

class AoRepQst extends QueryBuilder
{
    protected string $table = 'AO_REP_QST with (nolock)';
    protected string $primaryKey = '';
    protected array $primKey = [
        'NO_AO',
        'DTE_REP',
        'CODE_FOU',
        'TYPE_FG',
        'C_V', // This column is not actually used and should always be 'C'
        'ID_THEME',
        'ID_QUEST',
    ];
    
    // TODO : factorize with other QueryBuilder
    // Cascade delete MEMOS
    public function delete($id)
    {
        $inTrans = $this->pdo->inTransaction();
        if (!$inTrans) {
            $this->pdo->beginTransaction();
        }
        
        $param = [];
        $this->pdo->prepare(
            "DELETE MEMOS FROM MEMOS 
            JOIN AO_REP_QST 
                ON AO_REP_QST.NM_COM = MEMOS.NODEMEMO
                OR AO_REP_QST.REP_OUV_NM = MEMOS.NODEMEMO
            WHERE " . $this->getDeleteWhere($id, $param)
        )
        ->execute($param);

        $res = parent::delete($id);
        
        if (!$inTrans) {
            $this->pdo->commit();
        }

        return $res;
    }
}
