2016-03-28 22 views
0

Tüm crud işlevlerini içeren özel bir model (My_Model) oluşturdum. şimdi diğer modellerde genel model sınıfını miras almak istiyorum.Codeigniter'da bir modeli devralma

uygulama/çekirdek/My_Model.php

<?php 

class My_Model extends CI_Model { 

protected $_table; 
public function __construct() { 
    parent::__construct(); 
    $this->load->helper("inflector"); 
    if(!$this->_table){ 
     $this->_table = strtolower(plural(str_replace("_model", "", get_class($this)))); 
    } 
} 

public function get() { 
    $args = func_get_args(); 
    if(count($args) > 1 || is_array($args[0])) { 
     $this->db->where($args[0]); 
    } else { 
     $this->db->where("id", $args[0]); 
    } 
    return $this->db->get($this->_table)->row(); 
} 

public function get_all() { 
    $args = func_get_args(); 
    if(count($args) > 1 || is_array($args[0])) { 
     $this->db->where($args[0]); 
    } else { 
     $this->db->where("id", $args[0]); 
    } 
    return $this->db->get($this->_table)->result(); 
} 

public function insert($data) { 
    $success = $this->db->insert($this->_table, $data); 
    if($success) { 
     return $this->db->insert_id(); 
    } else { 
     return FALSE; 
    } 
} 

public function update() { 
    $args = func_get_args(); 
    if(is_array($args[0])) { 
     $this->db->where($args[0]); 
    } else { 
     $this->db->where("id", $args[0]); 
    } 
    return $this->db->update($this->_table, $args[1]); 
} 

public function delete() { 
    $args = func_get_args(); 
    if(count($args) > 1 || is_array($args[0])) { 
     $this->db->where($args[0]); 
    } else { 
     $this->db->where("id", $args[0]); 
    } 
    return $this->db->delete($this->_table);   
} 

} 

?> 

uygulama/model/user_model.php

<?php 

class User_model extends My_Model { } 

?> 

uygulama/kontrol/users.php

<?php 

class Users extends CI_Controller { 

public function __construct() { 
    parent::__construct(); 
    $this->load->model("user_model"); 
} 

function index() { 

    if($this->input->post("signup")) { 
     $data = array(
       "username" => $this->input->post("username"), 
       "email" => $this->input->post("email"), 
       "password" => $this->input->post("password"), 
       "fullname" => $this->input->post("fullname") 
      ); 
     if($this->user_model->insert($data)) { 
      $this->session->set_flashdata("message", "Success!"); 
      redirect(base_url()."users"); 
     } 
    } 
    $this->load->view("user_signup"); 
} 

} 

?> 

i yük denetleyicide satırı rahatsız edersem denetleyici 500 iç sunucu hatası aldım ancak - $ this-> load-> model ("user_model"); oluyor whats sonra görünümü sayfa yüklendiğinde, ...

CI yapılandırma dosyasında
+0

user_model'deki kaba işlevi kullandım ... iyi çalışıyor ... ama tüm ham fonksiyonları my_model'e koyduğumda ... çalışmıyor ... my_model user_model öğesinden devralınmıyor .. –

cevap

2

bulmak '/ config/config.php uygulama' ve set yapılandırma öğesi .. plz yardım ... çözemiyorum

$config['subclass_prefix'] = 'My_';

daha sonra CI load_class işlevi, CI_Model ve My_model işlevini rutininizde çağırırken yükleyecektir;

+0

Zaten ayarlanmış ... –

+0

onun windows pc içinde çalışıyor ... benim linux sistemi ile neyin yanlış olduğunu bilmiyorum –

+0

cevabınız için teşekkür ederim –