Jump to content

Manipulate data of item in WireArray


chrizz
 Share

Recommended Posts

I have a WireArray which holds objects of type WireData. I use WireArray because I would like to utilize the capabilities of using selectors to find items. My current use case is to find one specific item, modify one value and put it back to the WireArray. 

The code (simplified) looks like this:

// fill the array
$arr = new WireArray();

$item1 = new WireData();
$item1->text = "lorem ipsum";
$item1->identifier = "123";
$arr->add($item1);

$item2 = new WireData();
$item2->text = "lorem ipsum";
$item2->identifier = "456";
$arr->add($item2);

$item3 = new WireData();
$item3->text = "lorem ipsum";
$item3->identifier = "789";
$arr->add($item3);


// use the array

$myItem = $arr->get("identifier=456");
$myItem->text = "some other text";


// update the item in the array

?



Is there any trick to store the modified item in the array again?

  • Removing the old item does not work because I don't know how to retrieve the key of $myItem
  • Adding the item just duplicates it
  • Replace the old item does not work - it keeps the old item

 

Any help is appreciated! I hope that there is a solution - otherwise I have to replace the whole construct with single pages (which would be overkill)

 

Thanks a lot!

 

Link to comment
Share on other sites

28 minutes ago, chrizz said:

Is there any trick to store the modified item in the array again?

Are your tests not working? It works here as expected. When I loop through $arr, the text for item with identifier=456  is "some other text";

modify-wirearray.thumb.png.9d575998fa266d63a7b72ed244ce2d01.png

Link to comment
Share on other sites

nahhh.... I found the issue. If I would have followed @LostKobrakai advice: serialize can cause unforseen issues.

The above example works fine, but actually (and I didn't test this yesterday) I (un-)serialize the array before replacing one item. When I use the example and serialize WireArray before getting and replacing the element it simply does nothing but it shows the old item

I guess I have to go with the "use pages for every item" solution which is much less error prone. 

thanks @kongondo for testing this

 

Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...