控制器中的Codeigniter负载模型 - 500错误

时间:2013-03-20 10:46:49

标签: codeigniter model

这是我的控制者:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');


class Order extends CI_Controller {

public function index($id=-1) {
  $this->load->model('order');
}
}

当我尝试打开相应的URL时,我得到错误500.奇怪的是,我在另一个控制器上以相同的方式加载模型没有问题。

以下是路线和模型以防万一:

路线:

$route['order/(:num)'] = "order/index/$1";

型号:

<?php
    class Order extends CI_Model {

        var $fullname = '';
        var $email = '';
        var $address = '';
        var $phone = '';
        var $notes = '';
        var $facebook = '';
        //var $canvases = '';

        var $admin_notes = '';
        var $status = '';

        var $id = '';
        var $date = '';
        var $price = '';

        //var $emailStatus_recivedOrder = '';
        //var $emailStatus_sendedOrder = '';
        //var $emailStatus_askFeedback = '';

        function __construct() {
            // Call the Model constructor
            parent::__construct();
        }

        function get($search_query='', $per_page=5, $skip=0) {

            if($search_query != '') {
                $this->db->or_where('id', $search_query);
                $this->db->or_where('email', $search_query);
                $this->db->or_where('fullname', $search_query);
                $this->db->or_where('phone', $search_query);
            }

            $query = $this->db->get('entries', $per_page, $skip);
            return $query->result();
        }

        function count_all($search_query='') {
            if($search_query != '') {
                $this->db->or_where('id', $search_query);
                $this->db->or_where('email', $search_query);
                $this->db->or_where('fullname', $search_query);
                $this->db->or_where('phone', $search_query);
            }

            $this->db->from('entries');
            return $this->db->count_all_results();
        }

        function get_by_id($id) {
            return $this->db->get_where('entries', array('id' => $id), 1);
        }

        function get_active_orders_count() {
            $this->db->where('status', '1');
            $this->db->from('entries');
            return $this->db->count_all_results();
        }

        function insert_entry() {
            $this->fullname = $this->input->post('fullname');
            $this->email = $this->input->post('email');
            $this->address = $this->input->post('address');

            $this->phone = $this->input->post('phone');

            $this->facebook = $this->input->post('facebook');

            $this->notes = $this->input->post('notes');
            $this->admin_notes = $this->input->post('admin_notes');
            $this->status = $this->input->post('status');

            $this->date   = date('Y-m-d H:i:s');

            $this->db->insert('entries', $this);
        }

        function update_entry() {
            $this->admin_notes = $this->input->post('admin_notes');

            $this->db->update('entries', $this, array('id' => $this->input->post('admin_notes')));
        }
    }

错误是: 发生数据库错误

无法使用提供的设置连接到数据库服务器。

文件名:core / Loader.php

行号:346

2 个答案:

答案 0 :(得分:2)

您无法为控制器命名并为其建模。 给他们一个不同的名字,问题应该修复。像

这样的东西

控制器

class Order extends CI_Controller{ ... }

模型

class Order_model extends CI_Model{ ... }

答案 1 :(得分:0)

在面向对象的编程中,没有两个类可以具有相同的名称。从codeigniter控制器开始,模型都是类。因此,最好将控制器命名为ABC_Controller,将模型命名为ABC_Model,以避免类名冲突。

控制器

class ABC_Controller extends CI_Controller { .. }

模型

class ABC_Model extends CI_Model { .. }
相关问题