Jump to content

Does add() from API automatically save() the page ???


celfred
 Share

Recommended Posts

Hello,

Just a simple (I think) question which is in the title of my post.
Roughly speaking : here's my code (made-up because my real function is so long... I don't want to post it all here ;) )

	function updateScore($player, $task, $real = true) {
	[...]
	$player->score = $player->score+$task->score;
	if ($task->name == 'new-equipment') {
	  $new-eq = $pages->get("name=sword");
	  $player->equipment->add($new-eq);
	}
	if ($real == true) {
	  $player->save();
	}
	}
	

Everything works fine when I call updateScore($player, $task, true), but if I call updateScore($player, $task, false), scores are untouched, but the equipment gets added !

It used to work fine on PW2.7 but my update to PW 3.0.62 seems to have broken this... Is there a simple explanation ? I keep reading my code over and over and this is driving me crazy...

Thanks !

Link to comment
Share on other sites

Thanks. But is that new in some way ? Or was it always like that but I didn't notice ?

Just making sure I'm being understood : the equipment is added to the player's page, BUT it stays in the equipment list even though I reload the page and empty the cache. I thought it wouldn't stay since I didn't save the page.

Link to comment
Share on other sites

10 hours ago, Zeka said:

If I'm not mistaken it saves field value, but not the whole page. 

https://github.com/processwire/processwire/blob/master/wire/core/PagesEditor.php#L113

@Zeka, that's a different method. That add() is for adding a new page to a specified parent and assigning it a given template. The add() @celfred is referring to is the WireArray method documented here and as seen here. It adds an item to the end of a WireArray (and its derivatives, e.g. PageArray).

11 hours ago, celfred said:

It used to work fine on PW2.7 but my update to PW 3.0.62 seems to have broken this... Is there a simple explanation ? I keep reading my code over and over and this is driving me crazy...

@celfred

It shouldn't add without a save. There's something funky going on in your code. It's hard to pinpoint where because we can't see the original code. Maybe there's some other save or $real is being overwritten somewhere. I've tested your pseudo function and it works as expected in both PW 2.7 and PW 3.0.71. If I pass $real as false, the $page is not added to the Page Field.

Btw, though made up, this variable name will throw an error  $new-eq

  • Like 3
Link to comment
Share on other sites

 @kongondo Sorry for being late on that... Thanks for your reply. Somehow, your answer is comforting since things should work the way I was understanding them. Now I'll try and find out what's going on in my code.

Good point about my wrong variable name. I wasn't even aware of that... Shame on me...

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

×
×
  • Create New...