Welcome Guest, Not a member yet? Register   Sign In
Use Library in Model
#1

Hello,
I'm creating a custom library, like this:
PHP Code:
<?php
defined
('BASEPATH') OR exit('No direct script access allowed');

class 
my_class {
        protected 
$SDP;
        public function 
__construct()
        {
            
$this->SDP =& get_instance();
            
$this->SDP->load->database();
        }
        public function 
test4()
        {
            
$query $this->SDP->db->get('unit');
            return 
$query->result_array();
        } 
and create this Model:
PHP Code:
<?php
class my_model extends CI_Model {

        public function 
__construct()
        {
          
$this->load->library('my_class');
        }
       public function 
test1()
        {
          
$q $this->my_class->test4();
          
var_dump($q);
        } 
Is my code follow the MVC and Codeigniter's standards for use Library and Model?
If it's wrong please help me to fix it.
Reply
Reply
#3

Personally, I would rarely use a library to access the database, but there are some rare exceptions (such as building a data access layer). If anything, I might load a model from my library to access the database, though some people would disagree about loading data from the database into a library at all except as arguments to the library's methods.

There is not one right way to do almost anything in CodeIgniter, which is one of the reasons for its popularity.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB