• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Access to model functions throught custom library

#1
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
Simplicity is the ultimate sophistication
Reply

#2
Hi!

Use get_instance()

Example https://stackoverflow.com/questions/6795...om-library
Reply

#3
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();
    }

What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#4
(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);
    }

Reply

#5
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
Reply

#6
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
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.