CodeIgniter, models, and ORM, how to deal with this?

后端 未结 10 836
谎友^
谎友^ 2021-02-03 19:11

I\'m starting with CodeIgniter and after several hours diving in Google I\'m a bit confused.

Let\'s try to explain my question with a easy example: I have a table \'car

10条回答
  •  一整个雨季
    2021-02-03 19:53

    You probably want something like this:

       class Cars {
    
        //List all neccessary vars here
    
        function __construct() {
            //get instance of Codeigniter Object and load database library
            $this->obj =& get_instance();
            $this->obj->load->database();
        }
    
    //You can now list methods like so:
    function selectCar($name, $color) {
    
            $this->obj->db->select('color')->from('car')->where('color', $color);
            $query = $this->obj->db->get();
    
            switch ($query->num_rows()) {
            case 0:
                return false;
                break;
            default:
                return $query->result();
                break;
            }
        }
    

    Hope that helps!

提交回复
热议问题