É tão bom saber que faço parecido com os bons :P

 

Só faço um pouco diferente, e acho legal partilhar convosco.

 

A tabela é sempre definida para a classe toda

 

protected $table = ‘’;

 

function __construct()

{

                $this->table =
$this->config->item(‘prefixo_das_tabelas’).’nome_da_tabela’;

}

 

Eu tenho uma function para fazer where

 

private function get_where( $where = array() )

{

                $this->db->where($where);

}

 

Assim sendo, eu não preciso de 2 functions para mostrar e mostrar_onde.

Fica uma só, assim

 

protected function mostrar( $where = FALSE )

{

                if ( $where !== FALSE )

                               $this->get_where( $where );

                //Andamento com a situation

                

                return $this->db->get($this->table);

}

 

Quanto as functions de manipulação, eu tento sempre retornar com o
affected_rows() ou insert_id();

Assim eu tenho certeza que a operação ocorreu certo. Ou errado :P

 

Mas de resto, é tal qual.

 

Abraços

 

De: [email protected]
[mailto:[email protected]] Em nome de Cliff Oliveira
Enviada em: terça-feira, 29 de junho de 2010 00:51
Para: CodeIgniter Brasil
Assunto: [CodeIgniter] CRUD generico

 

Saudaçoes

a lista já me ajudou muito, por isso posto aqui um CRUD genérico que usamos
aqui na Tribo
ele tem me ajudado em muita coisa e espero que seja útil tbm a alguém :

<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
*/
class crud_model extends Model {
    function _construct() {
        parent::Model();
        $this->load->database();
    }
    function inserir($table, $array) {
        $this->db->insert($table, $array);
        return true;
    }
    function atualizar($table, $id_name, $array) {
        $this->db->where($id_name,$array[$id_name]);
        $this->db->update($table, $array);
        return true;
    }
    function deletar($table, $id_name, $array) {
        $this->db->where($id_name,$array[$id_name]);
        $this->db->delete($table);
        return true;
    }
    function mostrar($table) {
        $query = $this->db->get($table);
        return $query;
    }
    function mostrar_onde($table, $array) {
        $query = $this->db->get_where($table, $array);
        return $query;
    }

    function mostrar_ordenado($table, $array, $col, $how) {
        $this->db->select('*');
        $this->db->from($table);
        $this->db->where($array);
        $this->db->order_by($col, $how);
        $query = $this->db->get();
        return $query;
    }


}
?>


-- 
Cliff Oliveira
www.tribodeideias.com.br

_______________________________________________
Lista mailing list
[email protected]
http://codeigniter.com.br/mailman/listinfo/lista_codeigniter.com.br

Responder a