<?php

namespace Entity\Model;

use Helpers\QueryBuilder\QueryBuilder;

class AoDaRep extends QueryBuilder
{
    protected string $table = 'AO_DAREP with (nolock)';

    protected string $primaryKey = '';

    protected array $primKey = [
        'NO_AO',
        'DTE_REP',
        'CODE_FOU',
        'TYPE_FG',
        'C_V',
    ];

    // 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_DAREP ON AO_DAREP.COMMENT1 = MEMOS.NODEMEMO
            WHERE " . $this->getDeleteWhere($id, $param)
        )
        ->execute($param);

        $res = parent::delete($id);
        

        if (!$inTrans) {
            $this->pdo->commit();
        }

        return $res;
    }
}
