![]() |
mysql_fetch_object - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: mysql_fetch_object (/showthread.php?tid=33183) |
mysql_fetch_object - El Forum - 08-18-2010 [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-fetch-object.php) when returning data using the CI ActiveRecord methods? mysql_fetch_object - El Forum - 08-18-2010 [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). mysql_fetch_object - El Forum - 08-18-2010 [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_Codeigniter_Library_No_Zend_No_Hooks_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). mysql_fetch_object - El Forum - 08-18-2010 [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. mysql_fetch_object - El Forum - 08-18-2010 [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. mysql_fetch_object - El Forum - 08-18-2010 [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. |