<?php namespace App\Models;

use App\Entities\PetCliente\PetCliente;

class PetClienteModel extends ModelEntity
{
    protected $table = 'pet_cliente';
    protected $primaryKey = 'id';
    protected $returnType = 'array';
    protected $allowedFields = [
        'id_pet',
        'id_cliente'
    ];
    protected $useSoftDeletes = false;
    protected $skipValidation = true;
    protected $useTimestamps = false;

    public function registerPetCliente (PetCliente &$pet) : void {
        $pet_data = $pet->toArray();
        unset($pet_data['id']);
        $id_new_pet = $this->insert($pet_data);
        $pet->setId($id_new_pet);

    }

    public function updatePetClienteByID (int $id_pet, PetCliente $pet) : bool {
        $data = $pet->toArray(true);
        if (count($data) > 0) {
            $binds = ['id_pet' => $id_pet];
            $sql = "
                UPDATE pet
            ";
            $sql .= $this->generateUpdateSets($data, $binds);
            $sql .= 'WHERE pet.id = :id_pet:';
            $query = $this->db->query($sql, $binds);
            $errors = $this->errors();
            return !$errors;
        } else {
            return true;
        }
    }
}
