Welcome Guest, Not a member yet? Register   Sign In
accessing database in a static function
#1

[eluser]yuccaplant[/eluser]
Hi,

I'm new to CI and kind of new to php. In a model, I want to a static funtion to get all my entries from the database. However I can't reference $this in a static function. Are there other ways to reference db->get('')?

Something like this:
Code:
public static function get_all()
    {
        $query = this->db->get('entries');
        $entries;
        foreach ($query->result() as $row)
        {
            $entries[] = new Entry($row->id,$row->title, $row->body);
        }
        return $entries;
    }
#2

[eluser]Jim OHalloran[/eluser]
[quote author="yuccaplant" date="1189178001"]However I can't reference $this in a static function. Are there other ways to reference db->get('')?[/quote]The get_instance function should give you what you're after. Try this:
Code:
public static function get_all()
    {
        $CI = &get;_instance();
        $query = $CI->db->get('entries');
        $entries;
        foreach ($query->result() as $row)
        {
            $entries[] = new Entry($row->id,$row->title, $row->body);
        }
        return $entries;
    }

The get_instance function will return the CI instance you're after, then you can use the return value instead of $this (i.e. $CI instead of $this in the above example).

Jim.
#3

[eluser]xwero[/eluser]
[quote author="yuccaplant" date="1189178001"]
Code:
$entries[] = new Entry($row->id,$row->title, $row->body);
[/quote]

That part of your code looks very strange. If you want to define an array you have to do it like this:

Code:
$entries[] = array($row->id,$row->title, $row->body);

if you want to return an array you could use result_array instead of result. There is no need to loop through the query object if you are going to change anything
#4

[eluser]yuccaplant[/eluser]
Hi Jim, that works fine, thanks. What does the get_instance function do? Does it create a new instance of CI or is it just a reference to the existing instance?
#5

[eluser]yuccaplant[/eluser]
[quote author="xwero" date="1189185757"][quote author="yuccaplant" date="1189178001"]
Code:
$entries[] = new Entry($row->id,$row->title, $row->body);
[/quote]

That part of your code looks very strange. If you want to define an array you have to do it like this:

Code:
$entries[] = array($row->id,$row->title, $row->body);

if you want to return an array you could use result_array instead of result. There is no need to loop through the query object if you are going to change anything[/quote]

I want to return an array of Entry - objects (Entry is my Model). I don't know whether this is the best way to do it(?). If I return Entry-object, I can do things like this:
my_entry->update(), my_entry->get_comments(), ... . Which I can't if I return an array or a query object.
#6

[eluser]xwero[/eluser]
Ok i have said nothing Smile
#7

[eluser]Jim OHalloran[/eluser]
[quote author="yuccaplant" date="1189190712"]Does it create a new instance of CI or is it just a reference to the existing instance?[/quote]Good question, I'd always assumed it was returning the existing CI instance, but I wasn't 100% sure. I've taken a quick look at te implementation behind get_instance to find out for you, and this is what I found:

There is a class called CI_Base which the base Controller class extends. CI_Base is implemented using the singleton pattern to ensure there's only ever one instance of it, and the get_instance function returns the current CI_Base instance.

In a nutshell, if you have "$CI = &get;_instance();" in a function any CI libraries which would have been available via the $this variable in your controller will be available via the $CI variable. Its a reference to the existing CI instance, not a new one.

Jim.




Theme © iAndrew 2016 - Forum software by © MyBB