<?php
namespace Modules\Cad_blog\Models;

use App\Models\BaseModel;

class Model_cad_blog extends BaseModel {
	public function getCadastros() {
		$table 			= "cad_blog";
		$builder  	= $this->db->table($table);
		
		$builder->select('cad_blog.*, cad_blog_categorias.titulo AS categoria');
		$builder->join('cad_blog_categorias', 'cad_blog_categorias.id = ' . $table . '.id_categoria', 'left');
		
		$query = $builder->get();
		return $query->getResult();
	}

	public function cadastrando() {
		$table 		= "cad_blog";
		$builder  = $this->db->table($table);
		$query    = $builder->insert($_POST);

		return $this->db->insertID();
	}

	public function alterar($id) {
		$table 		= "cad_blog";
		$builder  = $this->db->table($table)->where('id', $id);

		$query    = $builder->get();
		return $query->getRow();
	}

	public function alterando($id) {
		$table 		= "cad_blog";
		$builder  = $this->db->table($table);

		$builder->set('data_alteracao', date("Y-m-d H:i:s"));
		$builder->where('id', $id);

		$builder->update($_POST);
		return $id;
	}

	public function deletar($id) {
		$table 		= "cad_blog";
		$builder  = $this->db->table($table);
		$builder->where('id', $id);
		$builder->delete();
	}

	public function getCategorias() {
		$table 		= 'cad_blog_categorias';
		$builder 	= $this->db->table($table);
		
		$query = $builder->get();
		return $query->getResult();
	}
}