CodeIgniter Forums
[revised] How do you call a model's db method from within a static method within that same class? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: [revised] How do you call a model's db method from within a static method within that same class? (/showthread.php?tid=35389)



[revised] How do you call a model's db method from within a static method within that same class? - El Forum - 10-27-2010

[eluser]stormbytes[/eluser]
I'm writing a static function that checks if its argument, $url, already exists in the database. The static function is a method of the Asset_model, and looks something like this (only a static version):

Code:
Class Asset_model Extends Model()
{        
    function asset_dupecheck($url)
    {
        $this->db->select('asset_id, catalog_title, asset_title')->where('asset_url', $url);
        $row = $this->db->get('Assets')->row();
        
        return $row;
    }
}

$this in the above-code is meant to illustrate my objective. In fact, that's my problem, since I can't use $this within a static method.

So I ask:

1. What's the correct way to structure the code within the asset_dupecheck() function, so that it can access the db method of it's container-class? I know it has to reference 'self' in some way, but I'm not sure about the syntax and google's not my friend.

2. My understanding was that :: is used to access a static property or method by referring to the class as a whole.

eg. MyClass::myStaticMethod()

Why then, does this also work with ordinary methods?

eg. I create a regular method for say.. a model class. But then, I can call that method from within a Controller using Model_Name::Method_Name() same as $this->model_name->method_name()?

and 3.

If (2) Then what's the point of a static method altogether if I can use *any* class method, static or otherwise, but simply calling it on the Class like MyClass::myMethod() ?


Thanks for lookin!


[revised] How do you call a model's db method from within a static method within that same class? - El Forum - 10-27-2010

[eluser]tonanbarbarian[/eluser]
CI is designed NOT to be used statically
However there is a way around what you are doing

rathe than use $this-> to access teh database you need to make an instance of the controller and use that to access everything

Code:
Class Asset_model Extends Model()
{        
    function asset_dupecheck($url)
    {
        $CI =& get_instance();
        $CI->db->select('asset_id, catalog_title, asset_title')->where('asset_url', $url);
        $row = $CI->db->get('Assets')->row();
        
        return $row;
    }
}
using the get_instance() is how you grab a copy of the controller and you can then access any of the model, libraries etc
this works in model, libraries, helpers etc


[revised] How do you call a model's db method from within a static method within that same class? - El Forum - 10-27-2010

[eluser]bretticus[/eluser]
Just an optimization suggestion...

I'd store your URLs (unless you have GB worth of them!) in memory via APC or memcache. You can set a TTL on them so they expire and re-query the database once until the next timeout or reset them each time you add a URL (recommended.)