2016-03-28 10 views
0

Ich habe ein benutzerdefiniertes Modell (My_Model) erstellt, das alle Crud-Funktionen enthält. Jetzt möchte ich diese allgemeine Modellklasse in anderen Modellen erben.erben eines Modells in coderigniter

application/Kern/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);   
} 

} 

?> 

application/Modelle/user_model.php

<?php 

class User_model extends My_Model { } 

?> 

application/Controller/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"); 
} 

} 

?> 

wenn i die Last Controller bekomme ich einen 500 internen Serverfehler, aber wenn ich die Zeile in Controller auskommentieren - $ this-> load-> model ("user_model"); dann die Ansicht Seite geladen wird, ... kann nicht herausfinden, was geschieht ... plz help ..

+0

Ich habe die Crud-Funktion in user_model verwendet ... es funktioniert gut..aber wenn ich alle Crud-Funktionen in my_model ... es funktioniert nicht ... my_model wird nicht in user_model vererbt .. –

Antwort

2

In CI-Konfigurationsdatei 'application/config/config.php' gefunden und eingestellt Konfigurationselement

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

dann lädt die CI load_class Funktion CI_Model und My_model beim Aufruf $ths->load->model('user_model') in Ihrer Routine;

+0

war es bereits eingestellt ... –

+0

es funktioniert gut in Windows pc.dont wissen, was ist falsch mit meinem Linux-System –

+0

Vielen Dank für Ihre Antwort –