我的这个控制器和模型与Codeigniter低版本一起工作,但它不适用于新版本

时间:2013-05-25 13:26:02

标签: php codeigniter

如果我使用控制器它工作正常但如果我使用CI_Controller它会出现此错误

Fatal error: Call to undefined method CI_Input::load_query() in C:\wamp\www\films\application\controllers\documents.php on line 124

控制器

function display($query_id = 0, $sort_by = 'title', $sort_order = 'asc', $offset = 0) {

        $limit = 20;
        $data['fields'] = array('FID' => 'ID', 'title' => 'Title', 'category' => 'Category', 'date_added' => 'date_added', 'department' => 'department', 'type' => 'type');

        $this -> input -> load_query($query_id);

        $query_array = array('title' => $this -> input -> get('title'), 'category' => $this -> input -> get('category'), 'date_added_comparison' => $this -> input -> get('date_added_comparison'), 'date_added' => $this -> input -> get('date_added'), );

        $data['query_id'] = $query_id;

        $this -> load -> model('document_m');

        $results = $this -> document_m -> search($query_array, $limit, $offset, $sort_by, $sort_order);

        $data['documents'] = $results['rows'];
        $data['num_results'] = $results['num_rows'];

        // pagination
        $this -> load -> library('pagination');
        $config = array();
        $config['base_url'] = site_url("documents/display/$query_id/$sort_by/$sort_order");
        $config['total_rows'] = $data['num_results'];
        $config['per_page'] = $limit;
        $config['uri_segment'] = 6;
        $this -> pagination -> initialize($config);
        $data['pagination'] = $this -> pagination -> create_links();

        $data['sort_by'] = $sort_by;
        $data['sort_order'] = $sort_order;

        $data['category_options'] = $this -> document_m -> category_options();
        $this -> load -> view('header');
        $this -> load -> view('document_index.php', $data);
        $this -> load -> view('footer');
    }

    function search() {

        $query_array = array('title' => $this -> input -> post('title'), 'category' => $this -> input -> post('category'), 'date_added_comparison' => $this -> input -> post('date_added_comparison'), 'date_added' => $this -> input -> post('date_added'), );

        $query_id = $this -> input -> save_query($query_array);

        redirect("documents/display/$query_id");

    }

模型

function search($query_array, $limit, $offset, $sort_by, $sort_order) {

        $sort_order = ($sort_order == 'desc') ? 'desc' : 'asc';
        $sort_columns = array('FID', 'title', 'category', 'date_added', 'department', 'type');
        $sort_by = (in_array($sort_by, $sort_columns)) ? $sort_by : 'title';

        // results query
        $q = $this->db->select('FID, title, category, date_added, department, type')
            ->from('documents')
            ->limit($limit, $offset)
            ->order_by($sort_by, $sort_order);

        if (strlen($query_array['title'])) {
            $q->like('title', $query_array['title']);
        }
        if (strlen($query_array['category'])) {
            $q->where('category', $query_array['category']);
        }
        if (strlen($query_array['date_added'])) {
            $operators = array('gt' => '>', 'gte' => '>=', 'eq' => '=', 'lte' => '<=', 'lt' => '<');
            $operator = $operators[$query_array['date_added_comparison']];

            $q->where("date_added $operator", $query_array['date_added']);
        }

        $ret['rows'] = $q->get()->result();

        // count query
        $q = $this->db->select('COUNT(*) as count', FALSE)
            ->from('documents');

        if (strlen($query_array['title'])) {
            $q->like('title', $query_array['title']);
        }
        if (strlen($query_array['category'])) {
            $q->where('category', $query_array['category']);
        }
        if (strlen($query_array['date_added'])) {
            $operators = array('gt' => '>', 'gte' => '>=', 'eq' => '=', 'lte' => '<=', 'lt' => '<');
            $operator = $operators[$query_array['date_added_comparison']];

            $q->where("date_added $operator", $query_array['date_added']);
        }

        $tmp = $q->get()->result();

        $ret['num_rows'] = $tmp[0]->count;

        return $ret;
    }

    function category_options() {

        $rows = $this->db->select('name')
            ->from('category')
            ->get()->result();

        $category_options = array('' => '');
        foreach ($rows as $row) {
            $category_options[$row->name] = $row->name;
        }

        return $category_options;
    }
    function get_category(){
        $this->db->select()->from('category');
        $query=$this->db->get();
        return $query->result_array();
    }

}

查看

<?php echo form_open('documents/search'); ?>
        <table width="" border="0" cellpadding="0" cellspacing="0">
            <tr>
                <td>

            <?php echo form_input('title', set_value('title'), 'id="title"'); ?>
                </td>
                <td>

            <?php echo form_dropdown('category', $category_options, 
                set_value('category'), 'id="category"'); ?>
                </td>
                <td>


            <?php echo form_dropdown('date_added_comparison', 
                array('gt' => '>', 'gte' => '>=', 'eq' => '=', 'lte' => '<=', 'lt' => '<') , 
                set_value('date_added_comparison'), 'id="date_added_comparison"'); ?>
            <td>
            <?php echo form_input('date_added', set_value('date_added'), 'id="date_added"'); ?>
                </td>
                <td valign="top">
                    <input type="submit" value="search" class="btn"/>
                </td>
            </tr>
        </table>
    <?php echo form_close(); ?>

    <div>
        Found <?php echo $num_results; ?> document
    </div>

    <table class="table table-striped">
        <thead>
            <?php foreach($fields as $field_name => $field_display): ?>
            <th <?php if ($sort_by == $field_name) echo "class=\"sort_$sort_order\"" ?>>
                <?php echo anchor("documents/display/$query_id/$field_name/" .
                    (($sort_order == 'asc' && $sort_by == $field_name) ? 'desc' : 'asc') ,
                    $field_display); ?>
            </th>
            <?php endforeach; ?>
        </thead>

        <tbody>
            <?php foreach($documents as $row): ?>
            <tr>
                <?php foreach($fields as $field_name => $field_display): ?>
                <td>
                    <?php echo $row->$field_name; ?>
                </td>
                <?php endforeach; ?>
            </tr>
            <?php endforeach; ?>            
        </tbody>

    </table>

    <?php if (strlen($pagination)): ?>
    <div>
        Pages: <?php echo $pagination; ?>
    </div>
    <?php endif; ?>


table(documents{FID,title,category,date_added,department,type,active}
table(category{id,name}
table(ci_query{id,query_string}

这对于codeigniter工作得很好,如果我用户控制器我的意思是旧版本的代码点火器,但这个新的我下载它'不工作并给出我发布的错误。

提前致谢

1 个答案:

答案 0 :(得分:1)

你可以扩展输入类

 class MY_Input extends CI_Input {

        function __construct()
        {
            parent::__construct();
        }

       function load_query(){
      //here what the method did
      }
    }

将其保存为my_input.php并将其保存在system/libraries/