Part of the EllisLab Network
   
1 of 2
1
Using CI models outside of CI.
Posted: 19 June 2007 08:07 AM   [ Ignore ]  
Summer Student
Total Posts:  20
Joined  06-19-2007

I’m using the following instructions for using a CI model outside of CI:
http://codeigniter.com/wiki/Calling_CI_models_from_outside_script/

I have the three include files set up as described, as well as my main php file with the proper includes and such.  Here’s what my main php file contains (concerning the CI model):

32 // These file allows us to access CI models without having to go through CI
33 require_once($CONFIG['CIMODEL_PATH'].'ci_model_remote_open.php');
34
35
36
// Loading a DbOps model which will allow us to carry on all of our DB Operations from outside of CI.
37 // This is part of the ci model interface.
38 require_once(APPPATH."/models/DbOps.php");
39
40
41 $DB
= new DbOps;
42 $DB->_assign_libraries();
43 // The query() function will transform the CI response into a standard array of results for us
44 $arrResults = $DB->query('SELECT * FROM APPS');

( The closing file include is elsewhere )

My $DbOps->query() method naturally has a line saying $this->db->query(); which sends the SQL query off to the DB.  When it gets to that line, I get this error:

Fatal error: Call to a member function on a non-object in /var/www/vhosts/launchcms.com/httpdocs/dev/launchcms/system/ci/system/application/models/DbOps.php on line 35

Line 35 in DbOps.php is the previously mentioned:  $sqlResults = $this->db->query($strQuery);

Here’s the whole function for anyone who wants to see it:

29 // This will handle general queries to the DB.
30 function query($strQuery) {
31
32     $this
->load->database();
33
34     
// asking the DB for the information we need
35     $sqlResults = $this->db->query($strQuery);
36
37     
// initializing the array of results we will be returning
38     $arrResults = array();
39
40     
// Adding elements to the array of results from the sql response.
41     if ( $sqlResult->num_rows() > 0 ) {
42         
for ( $i = 0; $i < $sqlResult->num_rows(); $i ++ ) {
43             
// Adding every row as an array element in our resuls array
44             $arrResults[$i] = $sqlResults->row_array($i + 1);
45         }
46     }
47     
else {
48         $arrResults[0]
= "No Results Returned";
49     }
50
51
52     
return $arrResults;
53
54
55 }

Does anyone have any idea why this might be giving me this error?  Obviously the writer of the original wiki article wasn’t getting it so I’m thinking that it’s just something that I’m doing wrong.  Thanks for any help you can give!

Profile
 
 
Posted: 19 June 2007 11:19 AM   [ Ignore ]   [ # 1 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  747
Joined  02-05-2007

I don’t know the problem, but you did forget the brackets when calling the constructor:

$DB = new DbOps;
// should be $DB = new DbOps();

 Signature 

“I am the terror that flaps in the night”

Profile
 
 
Posted: 19 June 2007 07:16 PM   [ Ignore ]   [ # 2 ]  
Summer Student
Total Posts:  20
Joined  06-19-2007
Rick Jolly - 19 June 2007 11:19 AM

I don’t know the problem, but you did forget the brackets when calling the constructor:

$DB = new DbOps;
// should be $DB = new DbOps();

Thanks.  I fixed that.  Can’t believe I missed it…  tongue laugh

Anyway, the problem still remains, so apparently it wasn’t related.  :(

Profile
 
 
Posted: 19 June 2007 08:04 PM   [ Ignore ]   [ # 3 ]  
Summer Student
Total Posts:  20
Joined  06-19-2007

OK, fixed it.  Apparently $this->load->database() doesn’t work when you’re using this technique.  I set up the autoload.php file to autoload the database library and it worked like a charm….go fig.

I’d imagine that this problem is more widespread than just the database libraries.  If anyone ever has trouble with this when using other libraries, try autoloading them…

Other than that, this is a really cool way to use CI models outside of CI.  It’s working like a charm now.

Profile
 
 
Posted: 22 June 2007 10:48 PM   [ Ignore ]   [ # 4 ]  
Summer Student
Avatar
Total Posts:  18
Joined  02-14-2007

Hi Hickeroar,

I am currently relocating so can’t really test what I say (I am in a hotel lobby right now smile). When I did this, my

$this->load->database();


line was in the constructor and did not give trouble, but I do not think that’s where the problem is. Don’t have my computer with me so can’t test.
Anyway, I am not sure if you saw my comment in the wiki since it gets cut off, but I think in your case it would be much cleaner to do something like:

$CI->load->model('DbOps');
$arrResults = $CI->DbOps->query('SELECT * FROM APPS');


instead of:

// Loading a DbOps model which will allow us to carry on all of our DB Operations from outside of CI.
// This is part of the ci model interface.
require_once(APPPATH."/models/DbOps.php");

$DB = new DbOps;
$DB->_assign_libraries();
// The query() function will transform the CI response into a standard array of results for us
$arrResults = $DB->query('SELECT * FROM APPS');


If you are wondering why I complicated things in the first case is because I was working with amfphp(flash remoting) which instantiate it’s own classes so I needed more fine grained control.
Again, it’s a pity that a more experienced person in the community does not want to look at this since I am sure they would find mistakes and it does prove useful as you can attest smile.

best of luck,
Stephan

NOTE: with some functional programming technique such as closures, the technique would probably be much cleaner (php does not have them). Will think about it.

Profile
 
 
Posted: 23 June 2007 10:31 AM   [ Ignore ]   [ # 5 ]  
Summer Student
Total Posts:  20
Joined  06-19-2007

I think my problem was that I wasn’t using $this->load->database() in the constructor, but in the method itself…  In any case, the auto-include in the config file works great.

What’s amazing is that I also used your CI model “interface” yesterday to hook CI up to amfphp and it worked like a charm.  I just include the proper files and do a “class ClassName extends DbOps” and run the DbOps constructor from within my class.  It works so nice and so clean.

I’m also using the same interface from another angle for another part of the application.

“Mistakes” or not, this little project of yours has become a mission critical asset to me.  I definitely appreciate your effort.

Profile
 
 
Posted: 23 June 2007 09:12 PM   [ Ignore ]   [ # 6 ]  
Summer Student
Avatar
Total Posts:  18
Joined  02-14-2007

Thanks Hickeroar, i am glad it was useful to someone, your words make the effort worthwile smile.

Profile
 
 
Posted: 24 June 2007 02:26 PM   [ Ignore ]   [ # 7 ]  
Summer Student
Total Posts:  5
Joined  10-13-2006

Would love if you did elaborte a bit on how you did the amfphp/CI bridge. smile

Profile
 
 
Posted: 24 June 2007 04:47 PM   [ Ignore ]   [ # 8 ]  
Summer Student
Total Posts:  20
Joined  06-19-2007
CI edge - 24 June 2007 02:26 PM

Would love if you did elaborte a bit on how you did the amfphp/CI bridge. smile

I had to modify the example files that MagicDuck showed in his example a little bit to work with my paths, but you won’t need instructions if you start seeing error messages about the paths or something. wink  Most people probably won’t have to do that, but my path model needs to be a bit more “dynamic” to work with the app I’m building.

For amfphp:

1) I had my CI model created and ready to go (called it DbOps).

2) I created my amfphp class and class file in the services folder (I called it LaunchRPC (LaunchRPC.php)).  Basically this is my code for that class:

<?php

    
//----------------------------------------------------------------------------------------------------
    // CI MODEL OPENER START
    
    // These file allows us to access CI models without having to go through CI
    
require_once("path/to/ci_model_remote_open.php");
        
    
// Loading a DbOps model which will allow us to carry on all of our DB Operations from outside of CI.
    // This is part of the ci model interface.
    
require_once(APPPATH."/models/dbops.php");
    
    
// CI MODEL OPENER END
    //----------------------------------------------------------------------------------------------------



    // Create new service for PHP Remoting as Class
    
class LaunchRPC extends DbOps {
    
        
function LaunchRPC () {
            
// Calling the DbOps constructor which calls the Model constructor...etc...
            
parent::DbOps();
            
            
// Define the methodTable for this class in the constructor
            
$this->methodTable = array(    
                
"myMethod" => array( "description" => "Desc Here", "access" => "remote"),
            );
        
}
        
        
        
function myMethod () {
            
            
//Showing how you can call functions that are in the model.
            
$this->functionFromDbOpsModel();

        
}
        
    }
    
    
    
    
//----------------------------------------------------------------------------------------------------
    // CI MODEL CLOSER START
    
    // Not actually sure if I need this here, but it doesn't seem to hurt.  Everything works fine with or without it.
    
require_once("path/to/ci_model_remote_close.php");
    
    
// CI MODEL CLOSER END
    //----------------------------------------------------------------------------------------------------
    
?>

3) Notice how I’m saying that LaunchRPC extends DbOps as well as the calling of the DbOps constructor from within the LaunchRPC Constructor.

4) Notice also that you can call the functions in your model by just doing a $this->functionName() call.

Slicker than snot!  Hope this helps you!

Profile
 
 
Posted: 26 June 2007 01:39 PM   [ Ignore ]   [ # 9 ]  
Summer Student
Avatar
Total Posts:  18
Joined  02-14-2007

// Not actually sure if I need this here, but it doesn't seem to hurt.  Everything works fine with or without it.
    
require_once("path/to/ci_model_remote_close.php");

That’s because php closes one db connection by itself anyway. This is more important when you have more than one db connection open.

@Hickeroar: I thought the newer amfphp didn’t require a method table anymore. But I am not that up to date with it.

Profile
 
 
Posted: 26 June 2007 02:44 PM   [ Ignore ]   [ # 10 ]  
Summer Student
Total Posts:  20
Joined  06-19-2007

Yaknow, I’m not sure about that.  I’ll check into it though.  Not having the methodtable would be nice smile

Profile
 
 
Posted: 26 June 2007 04:25 PM   [ Ignore ]   [ # 11 ]  
Summer Student
Total Posts:  20
Joined  06-19-2007

I looked into it and if you don’t use the methodTable, you have to use a file that will automatically built the methodtable for you.  Unfortunately in its current release, that file only works in php5.  There’s a php4 version in the next release i think.

I also can’t find anywhere how/where you include the file which processes the methodtable for you.  Either I’m missing something in the documentation, or there just isn’t any for it.

I’ll only have 4 or 5 functions for what I need anyway, so it’s no biggie.

Profile
 
 
Posted: 17 August 2007 04:05 AM   [ Ignore ]   [ # 12 ]  
Summer Student
Total Posts:  9
Joined  04-05-2007

oh oh
I was working on amfphp using codeigniter models from outside.
Is there anyway to incorporate gateway.php with CI, so that the service requests are passed to the controller functions? So, we could use application libraries, models etc. more effectively and efficiently..

Please advice..

Profile
 
 
Posted: 17 August 2007 09:58 AM   [ Ignore ]   [ # 13 ]  
Summer Student
Total Posts:  20
Joined  06-19-2007

webqatesting-

I played around with that idea a little bit, but honestly I’m not sure.  I couldn’t get it to work for some reason and because of my setup, it was easier to just integrate CI into the service instead of the entire gateway.

Profile
 
 
Posted: 17 August 2007 02:25 PM   [ Ignore ]   [ # 14 ]  
Summer Student
Avatar
Total Posts:  18
Joined  02-14-2007

Yeah, I had a grand plan to do that: passing php variables to flash movies through javascript so they would be like views and stuff like that. But it got to clunky/did not have enough patientence to see it through. If somebody did come up with something like that though, it would be cool.

Profile
 
 
Posted: 18 August 2007 12:06 AM   [ Ignore ]   [ # 15 ]  
Summer Student
Total Posts:  9
Joined  04-05-2007

Because of the time limit, I have to go back to loadVars, sendAndLoad for now.
I am sure somebody will come up with an integration of amfphp with CI controllers.

Thanks for the fastest replies..

Profile
 
 
   
1 of 2
1
 
‹‹ question input to db      CSS, Images..... ››
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: 64450 Total Logged-in Users: 28
Total Topics: 80955 Total Anonymous Users: 0
Total Replies: 435674 Total Guests: 187
Total Posts: 516629    
Members ( View Memberlist )