Macrura Posted March 25, 2015 Share Posted March 25, 2015 came across a strange edge case; I'm creating a new WireArray to collect images from various pages, which all get output into a gallery. In this particular site, the client had several images all with the exact same name on different pages, and when those were imported into the WireArray, they are de-duplicated, even though they are different images on different pages, so those images were not showing. I worked around it by renaming the images on the fly, and i can use the custom upload names to prevent this from happening in the future, but it does seem like someone else might run into this, not realizing that the wireArray is assuming the same named images are the same on import; i guess maybe i could have imported them into the wireArray using this syntax? $key = 0; foreach($p->product_images as $image) { $productImages[$key] = $image; $key++; } Link to comment Share on other sites More sharing options...
martin Posted March 26, 2015 Share Posted March 26, 2015 You don't need to use the $key. foreach($p->product_images as $image) { $productImages[] = $image; } How were you adding them to the WireArray in the first place? Link to comment Share on other sites More sharing options...
Macrura Posted March 26, 2015 Author Share Posted March 26, 2015 I was using import - is that the reason why it was de-duplicating on the file name? this is the code $productImages = new WireArray(); foreach( $page->children as $p ) { $productImages->import($p->product_images); } $productImages->import($page->product_images_master); Link to comment Share on other sites More sharing options...
Wanze Posted March 26, 2015 Share Posted March 26, 2015 I was using import - is that the reason why it was de-duplicating on the file name? Yep. WireArray::import() des check if the value is already in the array, if so, it does not import it again. In your case, I guess the PageImage::toString() method returns the same filename, so ProcessWire assumes that it's the same image, although it's from a different page. What you could do is to add them to a normal PHP array and then use WireArray::setArray() to pass them (not tested): $productImages = new WireArray(); $temp = array(); foreach ($page->childern as $p) { $temp = array_merge($temp, $p->product_images->getArray()); } $productImages->setArray($temp); Cheers 1 Link to comment Share on other sites More sharing options...
Macrura Posted March 26, 2015 Author Share Posted March 26, 2015 I think i may actually require that these go directly into WireArray, because when they are being output, i am using a lot of the properties, like $image->page, and then all of the inherited fields of the page that the image was pulled from; if i were to put them into a plain array, i would lose the $page property... maybe there needs to be a parameter for import which allows duplicates, especially in the case of page images? EDIT: or maybe your example does preserve the properties, with the getArray() ? will have to get more up to speed on that.. Link to comment Share on other sites More sharing options...
Wanze Posted March 26, 2015 Share Posted March 26, 2015 Yep, the values of the array are still wire derived objects, so it should work. 1 Link to comment Share on other sites More sharing options...
Macrura Posted March 26, 2015 Author Share Posted March 26, 2015 cool... will do some testing and report back; wondering if this should be documented somewhere, because i can see people assuming that the WireArray would differentiate images with the same name if they were keyed to different pages.. Link to comment Share on other sites More sharing options...
Soma Posted March 27, 2015 Share Posted March 27, 2015 Why do you need WireArray? It can be a normal array and store images in there. Link to comment Share on other sites More sharing options...
Soma Posted March 27, 2015 Share Posted March 27, 2015 You would not loose anything. You can add objects to a normal array and later can access them and they're still a Pageimage with all its properties. Link to comment Share on other sites More sharing options...
Wanze Posted March 27, 2015 Share Posted March 27, 2015 Why do you need WireArray? It can be a normal array and store images in there. Why not? WireArray offers way more possibilities for manipulating/filtering data, which is always nice to have. Link to comment Share on other sites More sharing options...
Macrura Posted March 27, 2015 Author Share Posted March 27, 2015 for now i'm using the method that Wanze posted, and it is working fine... along the way of this project i may test it with a normal array.. 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