CodeIgniter Forums
Use Library in Model - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=11)
+--- Thread: Use Library in Model (/showthread.php?tid=62965)



Use Library in Model - SajadDP - 09-12-2015

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.


RE: Use Library in Model - ciadmin - 09-12-2015

See http://www.codeigniter.com/user_guide/general/styleguide.html


RE: Use Library in Model - mwhitney - 09-14-2015

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.