$page->setAndSave() method

Quickly set field value(s) and save to database

You can specify a single field and value, or an array of fields and values.

This method does not need output formatting to be turned off first, so make sure that whatever value(s) you set are not formatted values.

Examples

// Set and save the summary field
$page->setAndSave('summary', 'When nothing is done, nothing is left undone.');
// Set and save multiple fields
$page->setAndSave([
  'title' => 'It is Friday again',
  'subtitle' => 'Here is another new blog post',
  'body' => 'Hope you all have a great weekend!'
]);
// Update a 'last_login' field after every user login
$session->addHookAfter('loginSuccess', function($event) {
  $user = $event->arguments(0);
  $user->setAndSave('last_login', time());
});

Usage

// basic usage
$bool = $page->setAndSave($key);

// usage with all arguments
$bool = $page->setAndSave($key, $value = null, array $options = []);

Arguments

NameType(s)Description
keyarray, string

Field or property name to set, or array of one or more ['property' => $value].

value (optional)string, int, bool, object

Value to set, or omit if you provided an array in first argument.

options (optional)array

See Pages::save() for additional $options that may be specified.

Return value

bool

Returns true on success, false on failure

See Also


$page methods and properties

API reference based on ProcessWire core version 3.0.236

Latest news

  • ProcessWire Weekly #550
    In the 550th issue of ProcessWire Weekly we're going to check out the latest core updates, introduce a new third party module called PagefileMetadata, and more. Read on!
    Weekly.pw / 23 November 2024
  • Custom Fields Module
    This week we look at a new ProFields module named Custom Fields. This module provides a way to rapidly build out ProcessWire fields that contain any number of subfields/properties within them.
    Blog / 30 August 2024
  • Subscribe to weekly ProcessWire news

“To Drupal, or to ProcessWire? The million dollar choice. We decided to make an early switch to PW. And in retrospect, ProcessWire was probably the best decision we made. Thanks are due to ProcessWire and the amazing system and set of modules that are in place.” —Unni Krishnan, Founder of PigtailPundits