CodeIgniter Forums

Full Version: mysql_fetch_object
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]JonoB[/eluser]
I usually use a php class to create value objects for my database objects. Something like:

Code:
class UserVO
{
    public $id;
    public $email;
}

and I then return this as follows:
Code:
$data = mysql_query($sql);
while ($row = mysql_fetch_object($data, "UserVO"))
{
    $result[] = $row;
}
return $result;

I'm now trying to get to grips with CI and the ActiveRecord methods.

I realise that I could do something like the following, buts its a bit more clunky, especially with queries that contain a lot of fields.
Code:
foreach ($query->result_array() as $row)
{
    $result[$i] = new UserVO();
    $result[$i]->id = $row['id'];
    $result[$i]->email = $row['email'];
    $i++;
}
return $result;

So, my question: is there a way to get "class_names" (as per http://php.net/manual/en/function.mysql-...object.php) when returning data using the CI ActiveRecord methods?

El Forum

[eluser]WanWizard[/eluser]
It's a bit pointless to do this, you're just creating a class with properties. $query->result() will return that as well, no need to define separate classes (and don't use result_array(), and then convert the result back into an object).

El Forum

[eluser]JonoB[/eluser]
The goal isnt pointless...but maybe my method is. Hence the question.

I have integrated AMFPHP into CI using http://codeigniter.com/wiki/AMF_Codeigni...ooks_Etc./

The point is that AMF and Flex integrate very nicely using well defined VO's (including having to set an explicitType parameter in the VO).

El Forum

[eluser]WanWizard[/eluser]
I wasn't talking about the goal. Smile

I just mean that
Code:
$data = mysql_query($sql);
while ($row = mysql_fetch_object($data, "UserVO"))
{
    $result[$i] = new UserVO();
    $result[$i]->id = $row['id'];
    $result[$i]->email = $row['email'];
    $i++;
}
return $result;

gives the same result as
Code:
$query = $this->db->query($sql);
return $query->result()

Which is an array of objects, one per row in the resultset.

There is only a difference if UserVO contains methods(). In that case you don't have to do a "new UserVO()", as mysql_fetch_object() will make sure $row already contains an instantiated UserVO object.

I would still opt not to use direct mysql functions, but do
Code:
$query = $this->db->query($sql);
$result = array();
foreach ( $query->result() as $row )
{
    $result[] = new UserVO($row);
}
return $result;

and make sure the constructor of your UserVO class picks up the parameter, and assigns it's values to the correct properties.

El Forum

[eluser]JonoB[/eluser]
Ok cool, thanks for the response.

I guess that would work, although its a bit back-to-front comparred to how I've done it in the past. And, if I understand correctly, it means looping through the recordset twice.

Its a pity that I can't do something like:
Code:
$query = $this->db->query($sql, "UserVO");
return $query->result()

But, your scenario is workable'ish.

El Forum

[eluser]WanWizard[/eluser]
What you do mean by looping twice?

You could modify the core classes if you want to be able to pass an object name, but it requires a modification to both the database core classes and the driver classes. Imho not worth the trouble.