Sign in to follow this  
celfred

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

Recommended Posts

celfred    51

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 !

Share this post


Link to post
Share on other sites
celfred    51

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.

Share this post


Link to post
Share on other sites
kongondo    5,003
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 2

Share this post


Link to post
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

Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By zaib
      I've a page "blog" which have child pages (blog posts). Right now I've 11 pages (blog posts) and I'm fetching all the posts in my "blog" pages which displaying fine, Issue is only 10 results are showing this is how I'm fetching
      $entries = $pages->find("template=blog-entry"); foreach($entries as $entry){ <a href='{$entry->url}'>$entry->title</a> }  
    • By pppws
      hey there,
      i'm quite new to processwire but i'm having a great experience using it!
      right now i hit a point where i can't help myself out with google/searchfunction.
       
      to sketch the basic functions of my page:
      visitors can enter their e-mail in a form. when doing this pw creates a new page, using the e-mail's md5 hash as a name. the url of the page is sent to the user. the user now can edit the newly created page and can fill out fields like: name and year of birth (thanks to the docs those two already work like a charm!) but there are other fields like "residencies", which are repeaterfields containing three fields: city (text), country (text), current (checkbox). at my current version i can populate a new repeater field easily by using this code:
      $location = $page->locations->getNew(); $location->location = Munich; $location->country = Germany; $location->current = 1; $location->save(); $page->locations->add($location); now i want to populate the new reapter fields from input fields (using the simple form api). which leads me to my questions:
       
      1. can i somehow group/merge input fields to one "repeater input fields" right now i have:
      // create a text input -> locations $field = $modules->get("InputfieldText"); $field->label = __('City'); $field->attr('id+name','location'); $field->required = 1; $form->append($field); // append the field to the form // create a text input -> country $field = $modules->get("InputfieldText"); $field->label = __('Country'); $field->attr('id+name','country'); $field->required = 1; $form->append($field); // append the field to the form // create a checkbox -> locations $field = $this->modules->get('InputfieldCheckbox'); $field->attr('name', 'location_current'); $field->attr('autocheck', 1); $field->attr('uncheckedValue', 0); $field->attr('checkedValue', 1); $field->attr('value', $this->fname); $form->append($field); // append the field to the form 2. my desired layout for the form looks like this:

      by hitting the + button a new line shows. i'm wondering what's the best practice here. can is somehow use the above mentioned code itself as a repeater, or do i have to create several (uniqe) input fields in advance and hide them afterwards (javascript)?
       
      any help is appreciated!
      thanks!
    • By alexcapes
      Hi,
      I've been invited by Apple to use their 'News' service for a publication in the UK. https://www.apple.com/uk/news/
      A number of CMSs already have plugins to publish content via the API, however Processwire is not one (yet!)
      https://newsresources.apple.com/en/faq/66346243
      It's slightly beyond my technical abilities but would be very interested in anyone that is willing to look at developing a Processwire Module.
      (for reference here is the Apple News API documentation)
      Update: Chapter Three have created a PHP Apple News library here https://github.com/chapter-three/AppleNewsAPI
    • By louisstephens
      So I have a bit of code for ad management :
      <?php $ads = $pages->find("parent.template=client, sort=expiration_date"); $alert_count = 0; foreach ($ads as $ad) { $todaysdate = date("F j, Y H:i"); $today = strtotime($todaysdate); $expireson = $ad->expiration_date; $expires = strtotime($expireson); $fiveaway = $expires - 432000; if ($today > $expires) { $alert = $alert_count=+1; echo $alert; } } //end FOREACH ?> It currently finds all the pages with a parent of "client" and then I can drill down to the pages that have "Expired" in my if statement. I wanted to get a "count" of the pages that met the if statement requirements so I could output that number in an alert at the top of the page. When it runs, it currently just prints out "1 1 1 1..." and not the total count of pages. Does anyone know of a way possibly achieving my desired output? I tried count(), but that did not quite produce my desired output.
       
      I should note that I have several other if statements dealing with the date/time for outputting other alerts as well (just didn't think they were needed for this case).
    • By Dennis Spohr
      Hi all,
      in the backend of ProcessWire it's possible to define a maximum width and height for images. If you upload an image, it will be resized automatically. I find this feature very handy to safe space. Very often users upload images which are much bigger than needed.
      On my application users can upload their images via the frontend. But if you upload images using the API, images won't be resized automatically.
      Is there any way to do this?
      Currently that's my code:
      if ($_FILES['thumbnail']['name']) { $upload = new WireUpload('thumbnail'); $upload->setMaxFiles(1); $upload->setOverwrite(true); $upload->setDestinationPath($lesson->video_image->path()); $upload->setValidExtensions(array('jpg', 'jpeg', 'png')); $lesson->video_image->removeAll(); foreach ($upload->execute() as $file) $lesson->video_image->add($file); } Thanks very much!
      Dennis