<?php

namespace App\Models;

use CodeIgniter\Model;

class GenericModel extends Model
{
    protected $useAutoIncrement = true;
    protected $returnType       = 'object';
    protected $useSoftDeletes   = false;
    protected $protectFields    = true;
    protected $useTimestamps = false;

    function getDataTable($columns,$order = null){
        if($order == null){
            $order = $this->primaryKey . " DESC";
        }
        return $this->asObject()->select($columns)->orderBy($order)->where($this->getColumnaEstado(),true);
    }

    function getPrefijo(){
        return explode("_",$this->primaryKey)[0];
    }

    function getColumnaEstado(){
        return $this->getPrefijo().'_estado';
    }

    function getColumnaUuid(){
        return $this->getPrefijo().'_uuid';
    }

    function getByUuid($uuid,$columna = null){
        if($columna == null){
            $columna = $this->getColumnaUuid();
        }
        return $this->where($columna, $uuid)->first();
    }
}