Tuesday, April 5, 2011

how to return the current object?

in code igniter you can type:

$query = $this->db->query("YOUR QUERY");

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

i guess that the query method returns the object it's part of. am i correct?

if i am, how do you type the line where it returns the object?

so what i wonder is how it looks like inside the query method for the above code to be functional.

public function query($sql) {
    // some db logic here with the $sql and saves the values to the properties (title, name and body)

    return X
}

with other words, what should X be?

From stackoverflow
  • I havn't read the CodeIgniter source code, but it might be reasonable to assume that $query is some kind of class representing a db result, so it might very well be a return $this;

    It could also be something like: return new Db_ResultSet($data) or similar.

  • That function is located in /system/database/DB_driver.php:244

    On a successful query, it returns a new db result object for the database driver (probably) set in your application's database config.

    ex: CI_DB_mysql_result (located in /system/database/drivers/mysql/mysql_result.php );

    with other words, what should X be?

    $driver         = $this->load_rdriver();
    $RES            = new $driver();
    $RES->conn_id   = $this->conn_id;
    $RES->result_id = $this->result_id;
    

    from DB_driver.php:366

0 comments:

Post a Comment