DataMapper ORM Question... |
[eluser]jordanarseno[/eluser]
Hey everyone. Before I begin, massive thanks to everyone involved with the DM ORM package, it's going to save me a lot of time while coding, and will also allow for a much more readable codebase. When using the get() or get_iterated() methods in DM, the result contains a huge amount of data, including the simple result set. A var_dump() or print_r() on the object after a successful get() or get_iterated() will show tonnes of extra details regarding the object. I don't need all of this excess data, and in fact, it's causing me problems because of the kind of structure that my views are expecting; When information is pushed to a view, I ensure it is sent as an array of objects, this helps my designers with the templating engine. I can see at the bottom of the huge result set that the actual database data is indeed an array of objects, but there's so much other data above it. I need to either restrict the results, or extract the actual database results from this hugely populated object and then hand it to the view. I could, in theory, map each field to a separate object, group them in an array and pass them into the view, but this seems like overhead. Any ideas? Cheers and Thanks, JoAr
[eluser]WanWizard[/eluser]
The objects don't use that much memory, they all share a common class definition. Offcourse, if you run a query that returns 1000 rows, you'll get 1000 objects. That's the way an ORM works. If you only retrieve them to iterate over them, use get_iterated(), which only fetches one at the time, and doesn't store all results in memory. And always query exactly what you need, don't just pull the database and then ignore the rest. To convert objects to array's, use the array extension, which gives you the methods to_array() which converts the current object, and all_to_array() which converts all query results. Nothing stops you from converting the array extension to an object extension, which returns a data object or array of objects. You can also add this as a feature request on bitbucket. This is the way Datamapper works, it uses object models which encapsulate the data. However, there is no difference in handling the object between a DM object, and an object containing only data.
[eluser]jordanarseno[/eluser]
Hey WW, Thanks for your support so far - I will try the to_array() and all_to_array() extensions. Where are these located? Must I add the extension? Are they included in the current version of the library? Can't seem to find any documentation.
[eluser]WanWizard[/eluser]
Check the manual, there's a section on extensions in the ToC: - How to use them: http://datamapper.wanwizard.eu/pages/extensions.html - Included extensions: http://datamapper.wanwizard.eu/pages/extlist.html You can load extensions globally, or on a per-model basis. They will add extra methods to the model.
|
Welcome Guest, Not a member yet? Register Sign In |