1

我想在 codeigniter 模型中创建一个 SQL 视图。这样做的最佳方法是什么?

4

1 回答 1

0

我根据需要工作的表使用多个模型

应用程序/核心/MY_model.php

<?php
/**
 * CRUD Model
 * A base model providing CRUD, pagination and validation.
 */

class MY_Model extends CI_Model
{


    public $table;
    public $primary_key;
    public $default_limit = 15;
    public $page_links;
    public $query;
    public $form_values = array();
    protected $default_validation_rules = 'validation_rules';
    protected $validation_rules;
    public $validation_errors;
    public $total_rows;
    public $date_created_field;
    public $date_modified_field;
    public $native_methods = array(
        'select', 'select_max', 'select_min', 'select_avg', 'select_sum', 'join',
        'where', 'or_where', 'where_in', 'or_where_in', 'where_not_in', 'or_where_not_in',
        'like', 'or_like', 'not_like', 'or_not_like', 'group_by', 'distinct', 'having',
        'or_having', 'order_by', 'limit'
    );

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

    public function __call($name, $arguments)
    {
        call_user_func_array(array($this->db, $name), $arguments);

        return $this;
    }

    /**
     * Sets CI query object and automatically creates active record query
     * based on methods in child model.
     * $this->model_name->get()
     */
    public function get($with = array(), $include_defaults = true)
    {
        if ($include_defaults) {
            $this->set_defaults();
        }

        foreach ($with as $method) {
            $this->$method();
        }

        $this->query = $this->db->get($this->table);

        return $this;
    }

    /**
     * Query builder which listens to methods in child model.
     * @param type $exclude
     */
    private function set_defaults($exclude = array())
    {
        $native_methods = $this->native_methods;

        foreach ($exclude as $unset_method) {
            unset($native_methods[array_search($unset_method, $native_methods)]);
        }

        foreach ($native_methods as $native_method) {
            $native_method = 'default_' . $native_method;

            if (method_exists($this, $native_method)) {
                $this->$native_method();
            }
        }
    }

    /**
     * Call when paginating results.
     * $this->model_name->paginate()
     */
    public function paginate($with = array())
    {
        $uri_segment = '';
        $offset = 0;
        $per_page = $this->default_limit;

        $this->load->helper('url');
        $this->load->library('pagination');

        $this->set_defaults();

        foreach ($with as $method) {
            $this->$method();
        }

        $this->total_rows = $this->db->count_all_results($this->table);

        $uri_segments = $this->uri->segment_array();

        foreach ($uri_segments as $key => $segment) {
            if ($segment == 'page') {
                $uri_segment = $key + 1;

                if (isset($uri_segments[$uri_segment])) {
                    $offset = $uri_segments[$uri_segment];
                }

                unset($uri_segments[$key], $uri_segments[$key + 1]);

                $base_url = site_url(implode('/', $uri_segments) . '/page/');
            }
        }

        if (!$uri_segment) {
            $base_url = site_url($this->uri->uri_string() . '/page/');
        }

        $config = array(
            'base_url' => $base_url,
            'uri_segment' => $uri_segment,
            'total_rows' => $this->total_rows,
            'per_page' => $per_page
        );

        if ($this->config->item('pagination_style')) {
            $config = array_merge($config, $this->config->item('pagination_style'));
        }

        $this->pagination->initialize($config);

        $this->page_links = $this->pagination->create_links();

        /**
         * Done with pagination, now on to the paged results
         */
        $this->set_defaults();

        foreach ($with as $method) {
            $this->$method();
        }

        $this->db->limit($per_page, $offset);

        $this->query = $this->db->get($this->table);

        return $this;
    }

    function paginate_api($limit, $offset)
    {
        $this->set_defaults();
        if (empty($limit)) {
            $limit = $this->default_limit;

        }
        if (empty($offset)) {
            $offset = 0;
        }
        $this->db->limit($limit, $offset);
        return $this->db->get($this->table)->result();
    }

    /**
     * Retrieves a single record based on primary key value.
     */
    public function get_by_id($id, $with = array())
    {
        foreach ($with as $method) {
            $this->$method();
        }

        return $this->where($this->primary_key, $id)->get()->row();
    }

    public function save($id = NULL, $db_array = NULL)
    {
        if (!$db_array) {
            $db_array = $this->db_array();
        }

        if (!$id) {
            if ($this->date_created_field) {
                $db_array[$this->date_created_field] = time();
            }

            $this->db->insert($this->table, $db_array);

//          $this->session->set_flashdata('alert_success', 'Record successfully created.');

            return $this->db->insert_id();
        } else {
            if ($this->date_modified_field) {
                $db_array[$this->date_modified_field] = time();
            }

            $this->db->where($this->primary_key, $id);
            $this->db->update($this->table, $db_array);

//          $this->session->set_flashdata('alert_success', 'Record successfully updated.');

            return $id;
        }
    }

    /**
     * Returns an array based on $_POST input matching the ruleset used to
     * validate the form submission.
     */
    public function db_array()
    {
        $db_array = array();

        $validation_rules = $this->{$this->validation_rules}();

        foreach ($this->input->post() as $key => $value) {
            if (array_key_exists($key, $validation_rules)) {
                $db_array[$key] = $value;
            }
        }

        return $db_array;
    }

    /**
     * Deletes a record based on primary key value.
     * $this->model_name->delete(5);
     */
    public function delete($id, $others = array())
    {
        if (!empty($others)) {
            foreach ($others as $k => $v) {
                $this->db->where($k, $v);
            }
        } else {
            $this->db->where($this->primary_key, $id);
        }

        return $this->db->delete($this->table);

//      $this->session->set_flashdata('alert_success', 'Record successfully deleted.');
    }

    /**
     * Returns the CI query result object.
     * $this->model_name->get()->result();
     */
    public function result()
    {
        return $this->query->result();
    }

    /**
     * Returns the CI query row object.
     * $this->model_name->get()->row();
     */
    public function row()
    {
        return $this->query->row();
    }

    /**
     * Returns CI query result array.
     * $this->model_name->get()->result_array();
     */
    public function result_array()
    {
        return $this->query->result_array();
    }

    /**
     * Returns CI query row array.
     * $this->model_name->get()->row_array();
     */
    public function row_array()
    {
        return $this->query->row_array();
    }

    /**
     * Returns CI query num_rows().
     * $this->model_name->get()->num_rows();
     */
    public function num_rows()
    {
        return $this->query->num_rows();
    }

    /**
     * Used to retrieve record by ID and populate $this->form_values.
     * @param int $id
     */
    public function prep_form($id = NULL)
    {
        if (!$_POST and ($id)) {
            $this->db->where($this->primary_key, $id);
            $row = $this->db->get($this->table)->row();

            foreach ($row as $key => $value) {
                $this->form_values[$key] = $value;
            }
        }
    }

    /**
     * Performs validation on submitted form. By default, looks for method in
     * child model called validation_rules, but can be forced to run validation
     * on any method in child model which returns array of validation rules.
     * @param string $validation_rules
     * @return boolean
     */
    public function run_validation($validation_rules = NULL)
    {
        if (!$validation_rules) {
            $validation_rules = $this->default_validation_rules;
        }

        foreach (array_keys($_POST) as $key) {
            $this->form_values[$key] = $this->input->post($key);
        }

        if (method_exists($this, $validation_rules)) {
            $this->validation_rules = $validation_rules;

            $this->load->library('form_validation');

            $this->form_validation->set_rules($this->$validation_rules());

            $run = $this->form_validation->run();

            $this->validation_errors = validation_errors();

            return $run;
        }
    }

    /**
     * Returns the assigned form value to a form input element.
     * @param type $key
     * @return type
     */
    public function form_value($key)
    {
        return (isset($this->form_values[$key])) ? $this->form_values[$key] : '';
    }

}

然后当我需要特定数据库的模型时,我在模型中使用类

模型/内容.php

class content_types extends MY_Model
{
    function __construct()
    {
        parent::__construct();
        $this->curentuserdata = $this->session->userdata('lg_result');
        $this->userprofile = $this->session->userdata('user_profile');
    }

    public $table = 'content_types';
    public $primary_key = 'id';
    public $default_limit = 50;


    public function default_order_by()
    {
        $this->db->order_by('id asc');
    }


}

那么我需要在哪里调用模型

$this->modelname->save($id,array());
$this->modelname->where()->get()->row();

所以为“视图表”创建一个,为插入表创建另一个

于 2013-02-28T03:52:18.327 回答