mysql_fetch_object |
[eluser]JonoB[/eluser]
I usually use a php class to create value objects for my database objects. Something like: Code: class UserVO and I then return this as follows: Code: $data = mysql_query($sql); 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) 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?
[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).
[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).
[eluser]WanWizard[/eluser]
I wasn't talking about the goal. ![]() I just mean that Code: $data = mysql_query($sql); gives the same result as Code: $query = $this->db->query($sql); 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); and make sure the constructor of your UserVO class picks up the parameter, and assigns it's values to the correct properties.
[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"); But, your scenario is workable'ish.
[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. |
Welcome Guest, Not a member yet? Register Sign In |