2016-03-28 11 views
0

すべての機能を含むカスタムモデル(My_Model)を作成しました。今、私は他のモデルの一般的なモデルクラスを継承したい。はコードシニターでモデルを継承します

アプリケーション/コア/ 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);   
} 

} 

?> 

アプリケーション/モデル/ user_model.php

<?php 

class User_model extends My_Model { } 

?> 

アプリケーション/コントローラ/ 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がロードコントローラに500行の内部サーバエラーが発生するが、コントローラの行のコメントを外すと - $ this-> load-> model( "user_model");起こっていただきました!そして、 ビューページがロード、...

CIの設定ファイルで
+0

私はuser_modelでcrud関数を使用しています...それはうまく動作していますが、my_modelにすべてのcrud関数を置くと...動作しません... my_modelはuser_model ..で継承されていません –

答えて

2

見つける「/設定/ config.phpのアプリケーション」と設定し、設定項目.. plzは助ける...把握カント

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

ルーチンで$ths->load->model('user_model')を呼び出すと、CI load_class関数はCI_ModelMy_modelをロードします。

+0

すでに設定されています... –

+0

Windowsのpc.dontが正常に動作していますか? –

+0

答えがありがとうございます –

関連する問題