$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 #520
    In the 520th issue of ProcessWire Weekly we'll check out some of the latest additions to the ProcessWire module's directory, share some highlights from the latest weekly update from Ryan, and more. Read on!
    Weekly.pw / 27 April 2024
  • ProFields Table Field with Actions support
    This week we have some updates for the ProFields table field (FieldtypeTable). These updates are primarily focused on adding new tools for the editor to facilitate input and management of content in a table field.
    Blog / 12 April 2024
  • Subscribe to weekly ProcessWire news

“The end client and designer love the ease at which they can update the website. Training beyond how to log in wasn’t even necessary since ProcessWire’s default interface is straightforward.” —Jonathan Lahijani