Welcome Guest, Not a member yet? Register   Sign In
Create a custom object from DB result()?
#1

[eluser]Andy Ingham[/eluser]
Hi All,

An interesting question (hopefully!)...

If I process through query results using the following code:

Code:
foreach ($query->result() as $row) {
    ...
}

then each '$row' is an object (of type stdClass?).

However, if I want to create my own 'custom' object for each row, so that I can wrap methods around the data, then what's the best way of doing it?

I notice in the 'mysqli_result::fetch_object' php.net documentation that you have the option of specifying a class_name when retrieving a row as an object:

Quote:class_name

The name of the class to instantiate, set the properties of and return. If not specified, a stdClass object is returned.

...so maybe this gives a clue as to an approach (although the CI result() function doesn't seem to accept any arguments).

Another approach is presumably to retrieve the row into a stdClass object, as described earlier, but then to copy the attributes from the stdClass object to my own class. If so, anyone know how best way to do this?

Any help on this greatly appreciated.

Thanks,

Andy


Messages In This Thread
Create a custom object from DB result()? - by El Forum - 11-27-2010, 09:48 AM
Create a custom object from DB result()? - by El Forum - 11-27-2010, 10:38 AM
Create a custom object from DB result()? - by El Forum - 11-27-2010, 02:28 PM
Create a custom object from DB result()? - by El Forum - 11-27-2010, 02:55 PM
Create a custom object from DB result()? - by El Forum - 11-27-2010, 05:13 PM
Create a custom object from DB result()? - by El Forum - 11-27-2010, 05:43 PM
Create a custom object from DB result()? - by El Forum - 12-02-2010, 05:55 PM



Theme © iAndrew 2016 - Forum software by © MyBB