Jim Yost Posted April 8, 2011 Share Posted April 8, 2011 Hey Ryan, Is there a way we can use getArray() so that it pulls all data from a page object rather than what is currently loaded into the object? For instance, exporting results to a CSV. I don't want to auto-join every field in my template just to get getArray to have everything already. I currently work around this by doing a foreach($page as $value) {} and then I can pull all the data with getArray() (Since now all field data is loaded). I don't mind doing the foreach before getArray(), however it was just very confusing at first when I didn't get an array with all of my values, only what was currently loaded. -Jim Link to comment Share on other sites More sharing options...
ryan Posted April 8, 2011 Share Posted April 8, 2011 Hey Jim, getArray() is a lower-level internal method (inherited from WireData) that's not intended to be part of the external $page API (http://processwire.com/api/variables/page/). Instead, getArray() just returns a picture of what's loaded in the class at that time. So your workaround is actually a pretty good solution. But I think this may provide more what you are looking for: $a = $page->getIterator(); The above will return a PHP ArrayObject that you can iterate or index-dereference like an array. If you need a native PHP array, you can do this: $a = iterator_to_array($page->getIterator()); I think this might also work (though haven't checked to confirm): $a = (array) $page->getIterator(); Some fields will be objects (like Page references, files, images, comments). Most can be typecast as strings, which may be what you want to do in exporting a CSV. But if you want the full raw data for the field, you may want to look at doing this: <?php $a = array(); foreach($page as $name => $value) { if(is_object($value)) { $field = wire('fields')->get($name); $value = $field->type->sleepValue($page, $field, $value); } $a[$name] = $value; } That sleepValue() function essentially converts a field's data to the appropriate format for storage in a DB. That should leave your array has having only values consisting of strings, nulls, ints or arrays (no objects, hopefully). In addition to the data you get from the above, you may also want to add these into your CSV: $page->id $page-parent_id $page->templates_id $page->name $page->status $page->sort $page->sortfield (string) $page->template 1 Link to comment Share on other sites More sharing options...
Jim Yost Posted April 8, 2011 Author Share Posted April 8, 2011 Perfect, thanks. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now