Part of the EllisLab Network
   
 
Library class loaded, but I can’t access it from a model??
Posted: 05 January 2009 12:33 PM   [ Ignore ]  
Grad Student
Avatar
Rank
Total Posts:  87
Joined  03-09-2008

Code within my model:

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

My library object:

<?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??

Profile
 
 
Posted: 05 January 2009 12:40 PM   [ Ignore ]   [ # 1 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  101
Joined  11-16-2008

In the Loginmodel:

$this->load->library('Factory');
$factory = new Factory();
$user = $factory->getUser('DS_Admin');
 Signature 

Inside of every large program is a small program struggling to get out.

Profile
 
 
Posted: 05 January 2009 01:14 PM   [ Ignore ]   [ # 2 ]  
Grad Student
Avatar
Rank
Total Posts:  87
Joined  03-09-2008

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 :(

Profile
 
 
Posted: 05 January 2009 03:45 PM   [ Ignore ]   [ # 3 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  198
Joined  12-31-2007

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

$this->Factory->getUser('DS_Admin');
// instead of $this->factory->getUser('DS_Admin');
Profile
 
 
Posted: 05 January 2009 06:04 PM   [ Ignore ]   [ # 4 ]  
Summer Student
Avatar
Total Posts:  24
Joined  11-04-2008

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://codeigniter.com/forums/viewthread/101322/

Profile
 
 
Posted: 05 January 2009 10:45 PM   [ Ignore ]   [ # 5 ]  
Grad Student
Avatar
Rank
Total Posts:  87
Joined  03-09-2008

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.

Profile
 
 
Posted: 06 January 2009 02:42 AM   [ Ignore ]   [ # 6 ]  
Grad Student
Avatar
Rank
Total Posts:  49
Joined  06-04-2008

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:

<?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:

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

 Signature 

Gilbert Maloloy-on
web programmer
http://www.precisiontech1.com
http://www.mesuncomputers.com
sleeping is not an option

Profile
 
 
Posted: 06 January 2009 10:22 AM   [ Ignore ]   [ # 7 ]  
Grad Student
Avatar
Rank
Total Posts:  87
Joined  03-09-2008

@therealmaloy

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

Profile
 
 
Posted: 06 January 2009 04:03 PM   [ Ignore ]   [ # 8 ]  
Grad Student
Avatar
Rank
Total Posts:  49
Joined  06-04-2008

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.

 Signature 

Gilbert Maloloy-on
web programmer
http://www.precisiontech1.com
http://www.mesuncomputers.com
sleeping is not an option

Profile
 
 
   
 
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 719, on June 06, 2008 10:16 AM
Total Registered Members: 77577 Total Logged-in Users: 12
Total Topics: 101563 Total Anonymous Users: 3
Total Replies: 544414 Total Guests: 199
Total Posts: 645977    
Members ( View Memberlist )
Active Members:    albertaCrucialdbashyaljayrulezjohn lanzjove4015kusmayadilonglostcousinsimshaunstomphTofuMattyalambers