<?php
class Race_Material { 
    private $IDRace_material;
    private $IDRace;

    private $IDMaterial;

    //Load from ID
    public function loadFromId($id) {
        global $wpdb;

        $sql = "
            SELECT IDRace_material, IDRace, IDMaterial FROM cass_race_material WHERE IDRace_material = %d;
        ";

        $result = $wpdb->get_row($wpdb->prepare($sql, $id));
      
        if ($result) {
            $this->setIDRace_Material($result->IDRace_material);
            $this->setIDRace($result->IDRace);
            $this->setIDMaterial($result->IDMaterial);
            return true;
        } else {
            return false;
        }

    }

    //Update 
    public function update() {
        global $wpdb;

        $sql = "
            UPDATE cass_race_material
            SET IDRace=%d, IDMaterial=%d 
            WHERE IDRace_material = %d;
         ";
         $wpdb->query($wpdb->prepare($sql, $this->getIDRace(), $this->getIDMaterial(), $this->getIDRace_Material()));
    }

    public function selectAllRaceMaterialFromRace($IDRace) {
        global $wpdb;

        $sql = "
        SELECT IDRace_material, IDRace, IDMaterial  FROM cass_race_material WHERE IDRace =%d
        ";

        return $wpdb->get_results($wpdb->prepare($sql, $IDRace));
    }

    //Delete
    public function delete() {
        global $wpdb;

        $sql = "
            DELETE FROM cass_race_material 
                WHERE IDRace_material =%d;
         ";
         $wpdb->query($wpdb->prepare($sql, $this->getIDRace_Material()));
    }

    public function deleteAllRaceMaterialFromRace($IDRace) {
        global $wpdb;

        $sql = "
            DELETE FROM cass_race_material 
                WHERE IDRace =%d;
         ";
         $wpdb->query($wpdb->prepare($sql, $IDRace));
    }

    //Insert
    public function create() {
        global $wpdb;

        $sql = "
        INSERT INTO cass_race_material(IDRace, IDMaterial)
        VALUES(%d,%d);
         ";
         $wpdb->query($wpdb->prepare($sql, $this->getIDRace(), $this->getIDMaterial()));
    }

    // Getters
    public function getIDRace_Material() {
        return $this->IDRace_material;
    }

    public function getIDRace() {
        return $this->IDRace;
    }

    public function getIDMaterial() {
        return $this->IDMaterial;
    }
    // Setters
    public function setIDRace_Material($IDRace_material) {
        $this->IDRace_material = $IDRace_material;
    }


    public function setIDRace($IDRace) {
        $this->IDRace = $IDRace;
    }

    public function setIDMaterial($IDMaterial) {
        $this->IDMaterial = $IDMaterial;
    }
        
}