Welcome Guest, Not a member yet? Register   Sign In
Method Chaining Problem
#3

[eluser]Gyzm[/eluser]
In the interest of keeping it simple I've setup a test controller. So here is ACTUAL code:

Code:
class Test extends Controller{
    

    function works(){
        $this->db->select('*');
        $this->db->from('trips');
        
        $query = $this->db->get();
        
        foreach($query->result() as $row){
            echo $row->title;
        }
    }

    function does_not_work(){
        $this->db->select('*')->from('trips');
        $query = $this->db->get();

        foreach($query->result() as $row){
            echo $row->title;
        }

        // either way both should work
    }

    function also_works(){
        // this is not method chaining
        $this->db->select('*');
        $query = $this->db->get('trips');

        // this would be the equivalent of
        // $query = $this->db->get('trips');

        foreach($query->result() as $row){
            echo $row->title."<br>";
        }
    }
}

I wanted to see what was happening in DB_active_rec.php so I put a print_query() function in. I took everything up to and including $this->_compile_select(); in the get() function and when method chaining you only get "SELECT *" which is outputted by the error message. When you don't use method chaining the string is compiled properly.

Interestingly this does work:
Code:
function this_also_works(){
        $query = $this->db->select('title')->where('id', '2')->get('trips');
        //$query = $this->db->get();

        foreach($query->result() as $row){
            echo $row->title;
        }
    }

Even more interesting is this:
Code:
function this_also_works(){
        $query = $this->db->select('title')->from('trips')->where('id', '2')->get();
        //$query = $this->db->get();

        foreach($query->result() as $row){
            echo $row->title;
        }
    }

...
Ah well nevermind. I'll just rewrite everything like this.

Thanks


Messages In This Thread
Method Chaining Problem - by El Forum - 05-12-2010, 01:56 PM
Method Chaining Problem - by El Forum - 05-12-2010, 03:00 PM
Method Chaining Problem - by El Forum - 05-12-2010, 03:26 PM
Method Chaining Problem - by El Forum - 05-13-2010, 08:12 PM



Theme © iAndrew 2016 - Forum software by © MyBB