CodeIgniter Forums
Access to model functions throught custom library - 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: Access to model functions throught custom library (/showthread.php?tid=69807)



Access to model functions throught custom library - LEBOSSS - 01-19-2018

Hi everybody!
I am not strong in English so please excuse me in advance if it is not very clear.
I'm creating my own library for authentication of my users but the problem is that apparently, it is impossible to access the to models functions from the library.
So it's impossible for me to do something like
PHP Code:
$this->CI->zauth_model->get_by('email'$email

Is it me who does it badly or there is  another way to do it?
Thank you


RE: Access to model functions throught custom library - Elias - 01-19-2018

Hi!

Use get_instance()

Example https://stackoverflow.com/questions/6795929/codeigniter-access-model-from-library


RE: Access to model functions throught custom library - InsiteFX - 01-20-2018

Or you can add this method to a CodeIgniter helper .

PHP Code:
/**
 * ci ()
 *
 * Place in any _helper file and load it.
 * 
 * The CodeIgniter Super Object
 */
if ( ! function_exists('ci'))
{
    
/**
     * ci ()
     * -------------------------------------------------------------------
     *
     * Example: ci()->class->method();
     * 
     * @return CI_Controller
     */
    
function ci()
    {
        return 
get_instance();
    }




RE: Access to model functions throught custom library - dave friend - 01-20-2018

(01-19-2018, 11:22 AM)LEBOSSS Wrote: Hi everybody!
I am not strong in English so please excuse me in advance if it is not very clear.
I'm creating my own library for authentication of my users but the problem is that apparently, it is impossible to access the to models functions from the library.
So it's impossible for me to do something like
PHP Code:
$this->CI->zauth_model->get_by('email'$email

Is it me who does it badly or there is  another way to do it?
Thank you

That will work if CI has the right value. This is the usual way to do that.
PHP Code:
class Example
{
     protected 
$CI;

     public function 
__construct()
    {
          
$this->CI = & get_instance();
          $this->CI->load->model('zauth_model');
    }

    public function use_model()
    {
          $this->CI->zauth_model->get_by('email'$email);
    }




RE: Access to model functions throught custom library - Zeff - 02-01-2018

Hi LEBOSS,

You should indeed call the code igniter instance 'by reference': assigning by reference allows you to use the original Code Igniter object rather than creating a copy of it. Read more about it here

Best,

Zeff


RE: Access to model functions throught custom library - Martin7483 - 02-01-2018

This is a code piece from CI itself. It is used in the CI_Model.

PHP Code:
/**
 * __get
 *
 * Enables the use of CI super-global without having to define an extra variable.
 *
 * @access public
 * @param $var
 * @return mixed
 */
public function __get($var)
{
 
   return get_instance()->$var;


Just place it in any class that requires the use of the CI super object.
Advantage of this approach is that you can keep coding like you would in your controllers