Welcome Guest, Not a member yet? Register   Sign In
Library class loaded, but I can't access it from a model??
#1

[eluser]nikefido[/eluser]
Code within my model:

Code:
<?php
class Loginmodel extends Model {
    
    private $user;
    
    public function Loginmodel() {
        parent::Model();
        $this->load->library('Factory');
        $this->factory->getUser('DS_Admin');
    }    
}

My library object:
Code:
<?php

class Factory {

    public function __construct() {
        //nothing
        echo 'yay';
    }

    private function __autoload($classname) {
        require_once('system/application/libraries/DS/'.$classname.'php');
    }
    
    public function getUser($usertype) {
        switch ($usertype) {
            case 'DS_Admin' :
                return new DS_Admin;
            case 'DS_Broker' :
                return new DS_Broker;
            case 'DS_MasterBroker' :
                return new DS_MasterBroker;
            default :
                return false;
        }
    }
}

Now, the "yay" within my Factory constructor is getting outputted, so I know the library is being loaded.

However, when I attempt to access the "getUser()" function, I receive this error:

Fatal error: Call to a member function getUser() on a non-object in C:\wamp\www\broker\system\application\models\loginmodel.php on line 9


ergh - what am I missing??
#2

[eluser]jalalski[/eluser]
In the Loginmodel:

Code:
$this->load->library('Factory');
$factory = new Factory();
$user = $factory->getUser('DS_Admin');
#3

[eluser]nikefido[/eluser]
That's very contrary to what I was expecting -

I was under the impression that the loader class instantiated the object for you (and treated it as a singleton, so you always received the same class instance).

Additionally, the object is actually being instantiated (you can tell because the "yay" is outputted to the browser) so there is an instance of the object somewhere to be found...

I'd love to make my getUser a static function, but I'm sure there would be issues with my __autoload implementation Sad
#4

[eluser]simshaun[/eluser]
You are correct that CI loads the library into a singleton.

Not sure why jalalski is initiating the factory like that.
It would work I suppose, but is not the "CI" way of doing it.

One thing that comes to mind that you can try is this in your controller
Code:
$this->Factory->getUser('DS_Admin');
// instead of $this->factory->getUser('DS_Admin');
#5

[eluser]WanWizard[/eluser]
If you do a var_dump($this) or print_r($this), is the 'factory' variable present, and does it point to the object?

If the variable isn't there, then it looks very similar to what I described in http://ellislab.com/forums/viewthread/101322/
#6

[eluser]nikefido[/eluser]
that does sound similar after reading your thread -

The object is definitely getting instantiated. I'll have to perform more tests tomorrow when I can play around more.
#7

[eluser]therealmaloy[/eluser]
nikefido

i think the loading of the class is not properly instantiated as part of CI.... i've used your snippets and modified such to look like the following:

Code:
<?php
class Loginmodel extends Model {

    private $usertemp;

    var $CI;
    
    public function Loginmodel() {
        parent::Model();
        
        $this->CI =& get_instance();
        
        $this->CI->load->library('Factory');
        $this->setUser($this->CI->factory->getUser('DS_Admin'));
    }
    
    private function setUser($usertempvar){
        $this->usertemp = $usertempvar;
    }


}

i just added some functions to test it on my machine, i changed your private $user var to something else to prevent conflicts on my system Smile

as you can see how i tried to instantiate your class to be part of CI...

hope this works, tell us if this solves the problem.

what i'm getting now is the following result:

Code:
yay
Fatal error: Class 'DS_Admin' not found in D:\xampp\htdocs\devel\codeigniter\myproject\system\application\libraries\Factory.php on line 17

past your problem, it is now proceeding to your getUser() function, though I don't have the DS_xxx classes that's why i get this Smile
#8

[eluser]nikefido[/eluser]
@therealmaloy

Thanks, that method seems to work. Kind of lame, however, since it makes using composition in your models a tad annoying.
#9

[eluser]therealmaloy[/eluser]
nikefido

good that it's fine now Smile

yeah, i guess, just learned this techniques with the intermediate codes i've run into and just become familiarized with it. seems to be the easy way out if you are trying to make your own libraries and similar stuff.




Theme © iAndrew 2016 - Forum software by © MyBB