$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 #519
    In the 519th issue of ProcessWire Weekly we'll check out a new third party module called RockForms, introduce the latest ProcessWire core updates, and more. Read on!
    Weekly.pw / 20 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

“I am currently managing a ProcessWire site with 2 million+ pages. It’s admirably fast, and much, much faster than any other CMS we tested.” —Nickie, Web developer