Documentation

BasicSaveAction extends AbstractSaveAction
in package

Create or update one or more $ENTITIES.

Tags
inheritDoc

Table of Contents

Properties

$_debugOutput  : mixed
$_actionName  : string
$_entityName  : string
$chain  : array<string|int, mixed>
Additional api requests - will be called once per result.
$checkPermissions  : bool
Whether to enforce acl permissions based on the current user.
$debug  : bool
Add debugging info to the api result.
$defaults  : array<string|int, mixed>
Array of default values.
$language  : string
Preferred language (optional)
$match  : array<string|int, mixed>
Specify fields to match for update.
$records  : array<string|int, mixed>
Array of $ENTITIES to save.
$reload  : bool
Reload $ENTITIES after saving.
$version  : int
Api version number; cannot be changed.
$_arrayStorage  : array<string|int, mixed>
$_entityFields  : array<string|int, mixed>
$_id  : int
$_paramInfo  : array<string|int, mixed>
$_reflection  : ReflectionClass
$setter  : callable

Methods

__call()  : static|mixed
Magic function to provide automatic getter/setter for params.
__construct()  : mixed
Basic Save constructor.
__set()  : mixed
Strictly enforce api parameters
_run()  : mixed
We pass the writeRecord function an array representing one item to write.
addChain()  : $this
addDefault()  : $this
Set default value for a field.
addRecord()  : $this
Add one or more records to be saved.
entityFields()  : array<string|int, mixed>
Returns schema fields for this entity & action.
evaluateCondition()  : bool
This function is used internally for evaluating field annotations.
execute()  : Result
Invoke api call.
getActionName()  : string
getChain()  : array<string|int, mixed>
getCheckPermissions()  : bool
getDebug()  : bool
getDefaults()  : array<string|int, mixed>
getEntityName()  : string
getLanguage()  : string|null
getMatch()  : bool
getParamInfo()  : array<string|int, mixed>
Get documentation for one or all params
getParams()  : array<string|int, mixed>
Serialize this object's params into an array
getPermissions()  : array<string|int, mixed>
getRecords()  : array<string|int, mixed>
getReload()  : bool
offsetExists()  : bool
offsetGet()  : mixed
offsetSet()  : void
offsetUnset()  : void
paramExists()  : bool
reflect()  : ReflectionClass
setChain()  : $this
setCheckPermissions()  : $this
setDebug()  : $this
setDefaults()  : $this
setLanguage()  : $this
setMatch()  : $this
setRecords()  : $this
setReload()  : $this
setVersion()  : $this
addCallbackToDebugOutput()  : mixed
When in debug mode, this logs the callback function being used by a Basic*Action class.
checkRequiredFields()  : array<string|int, mixed>
Validates required fields for actions which create a new object.
formatWriteValues()  : mixed
Replaces pseudoconstants in input values
getIdField()  : string
getLanguageOptions()  : array<string|int, mixed>
Get available preferred languages.
getMagicProperties()  : array<string|int, mixed>
Get a list of class properties for which magic methods are supported.
getMatchFields()  : array<string|int, mixed>
Options callback for $this->match
getParamDefaults()  : array<string|int, mixed>
matchExisting()  : mixed
Find existing record based on $this->match param
updateRecords()  : array<string|int, mixed>
validateValues()  : mixed
writeRecord()  : array<string|int, mixed>
This Basic Save class can be used in one of two ways:

Properties

$chain

Additional api requests - will be called once per result.

protected array<string|int, mixed> $chain = []

Keys can be any string - this will be the name given to the output.

You can reference other values in the api results in this call by prefixing them with $.

For example, you could create a contact and place them in a group by chaining the GroupContact api to the Contact api:

Contact::create()
  ->setValue('first_name', 'Hello')
  ->addChain('add_a_group', GroupContact::create()
    ->setValue('contact_id', '$id')
    ->setValue('group_id', 123)
  )

This will substitute the id of the newly created contact with $id.

$checkPermissions

Whether to enforce acl permissions based on the current user.

protected bool $checkPermissions = TRUE

Setting to FALSE will disable permission checks and override ACLs. In REST/javascript this cannot be disabled.

$debug

Add debugging info to the api result.

protected bool $debug = FALSE

When enabled, $result->debug will be populated with information about the api call, including sql queries executed.

Note: with checkPermissions enabled, debug info will only be returned if the user has "view debug output" permission.

$defaults

Array of default values.

protected array<string|int, mixed> $defaults = []

These defaults will be merged into every $ENTITY in records before saving. Values set in records will override these defaults if set in both places, but updating existing $ENTITIES will overwrite current values with these defaults.

$language

Preferred language (optional)

protected string $language

This option will notify major localization subsystems (ts(), multilingual, etc) about which locale should be used for composing/formatting messaging.

This indicates the preferred language. The effective language is determined by Civi\Core\Locale::negotiate($preferredLanguage).

Tags
optionsCallback

getLanguageOptions

$match

Specify fields to match for update.

protected array<string|int, mixed> $match = []

The API will perform an update if an existing $ENTITY matches all specified fields.

Note: the fields named in this param should be without any options suffix (e.g. my_field not my_field:name). Any options suffixes in the $records will be resolved by the api prior to matching.

Tags
optionsCallback

getMatchFields

$records

Array of $ENTITIES to save.

protected array<string|int, mixed> $records = []

Should be in the same format as returned by Get.

Tags
required

$reload

Reload $ENTITIES after saving.

protected bool $reload = FALSE

By default this action typically returns partial records containing only the fields that were updated. Set reload to true to do an additional lookup after saving to return complete values for every $ENTITY.

$version

Api version number; cannot be changed.

protected int $version = 4

$_arrayStorage

private array<string|int, mixed> $_arrayStorage = []

$setter

private callable $setter

Function(array $item, BasicCreateAction $thisAction): array

Methods

__call()

Magic function to provide automatic getter/setter for params.

public __call(mixed $name, mixed $arguments) : static|mixed
Parameters
$name : mixed
$arguments : mixed
Tags
throws
CRM_Core_Exception
Return values
static|mixed

__construct()

Basic Save constructor.

public __construct(string $entityName, string $actionName[, callable $setter = NULL ]) : mixed
Parameters
$entityName : string
$actionName : string
$setter : callable = NULL

__set()

Strictly enforce api parameters

public __set(mixed $name, mixed $value) : mixed
Parameters
$name : mixed
$value : mixed
Tags
throws
Exception

_run()

We pass the writeRecord function an array representing one item to write.

public _run(Result $result) : mixed

We expect to get the same format back.

Parameters
$result : Result

addChain()

public addChain(string $name, AbstractAction $apiRequest[, string|int|array<string|int, mixed> $index = NULL ]) : $this
Parameters
$name : string

Unique name for this chained request

$apiRequest : AbstractAction
$index : string|int|array<string|int, mixed> = NULL

See civicrm_api4() for documentation of $index param

Return values
$this

addDefault()

Set default value for a field.

public addDefault(string $fieldName, mixed $defaultValue) : $this
Parameters
$fieldName : string
$defaultValue : mixed
Return values
$this

addRecord()

Add one or more records to be saved.

public addRecord(array<string|int, mixed> ...$records) : $this
Parameters
$records : array<string|int, mixed>
Return values
$this

entityFields()

Returns schema fields for this entity & action.

public entityFields() : array<string|int, mixed>

Here we bypass the api wrapper and run the getFields action directly. This is because we DON'T want the wrapper to check permissions as this is an internal op.

Tags
see
GetFields
throws
CRM_Core_Exception
Return values
array<string|int, mixed>

evaluateCondition()

This function is used internally for evaluating field annotations.

public static evaluateCondition(string $expr, array<string|int, mixed> $vars) : bool

It should never be passed raw user input.

Parameters
$expr : string

Conditional in php format e.g. $foo > $bar

$vars : array<string|int, mixed>

Variable name => value

Tags
throws
CRM_Core_Exception
throws
Exception
Return values
bool

getActionName()

public getActionName() : string
Return values
string

getChain()

public getChain() : array<string|int, mixed>
Return values
array<string|int, mixed>

getCheckPermissions()

public getCheckPermissions() : bool
Return values
bool

getDefaults()

public getDefaults() : array<string|int, mixed>
Return values
array<string|int, mixed>

getEntityName()

public getEntityName() : string
Return values
string

getLanguage()

public getLanguage() : string|null
Return values
string|null

getParamInfo()

Get documentation for one or all params

public getParamInfo([string $param = NULL ]) : array<string|int, mixed>
Parameters
$param : string = NULL
Return values
array<string|int, mixed>

of arrays [description, type, default, (comment)]

getParams()

Serialize this object's params into an array

public getParams() : array<string|int, mixed>
Return values
array<string|int, mixed>

getPermissions()

public getPermissions() : array<string|int, mixed>
Return values
array<string|int, mixed>

getRecords()

public getRecords() : array<string|int, mixed>
Return values
array<string|int, mixed>

offsetExists()

public offsetExists(mixed $offset) : bool
Parameters
$offset : mixed
Tags
inheritDoc
Return values
bool

offsetGet()

public & offsetGet(mixed $offset) : mixed
Parameters
$offset : mixed
Tags
inheritDoc
Attributes
#[ReturnTypeWillChange]

offsetSet()

public offsetSet(mixed $offset, mixed $value) : void
Parameters
$offset : mixed
$value : mixed
Tags
inheritDoc

offsetUnset()

public offsetUnset(mixed $offset) : void
Parameters
$offset : mixed
Tags
inheritDoc

paramExists()

public paramExists(string $param) : bool
Parameters
$param : string
Return values
bool

reflect()

public reflect() : ReflectionClass
Return values
ReflectionClass

setChain()

public setChain([array<string|int, mixed> $chain = ]) : $this
Parameters
$chain : array<string|int, mixed> =
Return values
$this

setCheckPermissions()

public setCheckPermissions(bool $checkPermissions) : $this
Parameters
$checkPermissions : bool
Return values
$this

setDebug()

public setDebug([bool $debug = ]) : $this

Enable/disable debug output

Parameters
$debug : bool =
Return values
$this

setDefaults()

public setDefaults([array<string|int, mixed> $defaults = ]) : $this

Array of defaults.

Parameters
$defaults : array<string|int, mixed> =
Return values
$this

setLanguage()

public setLanguage([string|null $language = ]) : $this
Parameters
$language : string|null =
Return values
$this

setMatch()

public setMatch([array<string|int, mixed> $match = ]) : $this

Specify fields to match for update.

Parameters
$match : array<string|int, mixed> =
Return values
$this

setRecords()

public setRecords([array<string|int, mixed> $records = ]) : $this

Set array of records to be saved.

Parameters
$records : array<string|int, mixed> =
Return values
$this

setReload()

public setReload([bool $reload = ]) : $this

Specify whether complete objects will be returned after saving.

Parameters
$reload : bool =
Return values
$this

addCallbackToDebugOutput()

When in debug mode, this logs the callback function being used by a Basic*Action class.

protected addCallbackToDebugOutput(callable $callable) : mixed
Parameters
$callable : callable

checkRequiredFields()

Validates required fields for actions which create a new object.

protected checkRequiredFields(mixed $values) : array<string|int, mixed>
Parameters
$values : mixed
Tags
throws
CRM_Core_Exception
Return values
array<string|int, mixed>

formatWriteValues()

Replaces pseudoconstants in input values

protected formatWriteValues(array<string|int, mixed> &$record) : mixed
Parameters
$record : array<string|int, mixed>
Tags
throws
CRM_Core_Exception

getLanguageOptions()

Get available preferred languages.

protected getLanguageOptions() : array<string|int, mixed>
Return values
array<string|int, mixed>

getMagicProperties()

Get a list of class properties for which magic methods are supported.

protected static getMagicProperties() : array<string|int, mixed>
Return values
array<string|int, mixed>

List of supported properties, keyed by property name. Array(string $propertyName => bool $true).

getMatchFields()

Options callback for $this->match

protected getMatchFields() : array<string|int, mixed>
Return values
array<string|int, mixed>

getParamDefaults()

protected getParamDefaults() : array<string|int, mixed>
Return values
array<string|int, mixed>

matchExisting()

Find existing record based on $this->match param

protected matchExisting(mixed &$record) : mixed
Parameters
$record : mixed

updateRecords()

protected updateRecords(array<string|int, mixed> $items) : array<string|int, mixed>
Parameters
$items : array<string|int, mixed>
Return values
array<string|int, mixed>

writeRecord()

This Basic Save class can be used in one of two ways:

protected writeRecord(array<string|int, mixed> $item) : array<string|int, mixed>
  1. Use this class directly by passing a callable ($setter) to the constructor.
  2. Extend this class and override this function.

Either way, this function should return an array representing the one new object.

Parameters
$item : array<string|int, mixed>
Tags
throws
NotImplementedException
Return values
array<string|int, mixed>

        
On this page

Search results