public function DataProviderDbQuery::update in RESTful 7.2
Update operation.
Parameters
mixed $identifier: The ID of thing to be updated.
mixed $object: The thing that will be set.
bool $replace: TRUE if the contents of $object will replace $identifier entirely. FALSE if only what is set in $object will replace those properties in $identifier.
Return value
array An array of structured data for the thing that was updated.
Overrides CrudInterface::update
File
- src/
Plugin/ resource/ DataProvider/ DataProviderDbQuery.php, line 289 - Contains \Drupal\restful\Plugin\resource\DataProvider\DataProviderDbQuery.
Class
Namespace
Drupal\restful\Plugin\resource\DataProviderCode
public function update($identifier, $object, $replace = FALSE) {
// Build the update array.
$save = FALSE;
$original_object = $object;
$id_columns = $this
->getIdColumn();
$record = array();
foreach ($this->fieldDefinitions as $public_field_name => $resource_field) {
/* @var ResourceFieldDbColumnInterface $resource_field */
if (!$this
->methodAccess($resource_field)) {
// Allow passing the value in the request.
unset($original_object[$public_field_name]);
continue;
}
$property = $resource_field
->getProperty();
// If this is the primary field, skip.
if ($this
->isPrimaryField($property)) {
continue;
}
if (isset($object[$public_field_name])) {
$record[$property] = $object[$public_field_name];
}
elseif ($replace) {
$record[$property] = NULL;
}
unset($original_object[$public_field_name]);
$save = TRUE;
}
// No request was sent.
if (!$save) {
throw new BadRequestException('No values were sent with the request.');
}
// If the original request is not empty, then illegal values are present.
if (!empty($original_object)) {
$error_message = format_plural(count($original_object), 'Property @names is invalid.', 'Property @names are invalid.', array(
'@names' => implode(', ', array_keys($original_object)),
));
throw new BadRequestException($error_message);
}
// Add the id column values into the record.
foreach ($this
->getIdColumn() as $index => $column) {
$record[$column] = current($this
->getColumnFromIds(array(
$identifier,
), $index));
}
// Once the record is built, write it.
if (!drupal_write_record($this
->getTableName(), $record, $id_columns)) {
throw new ServiceUnavailableException('Record could not be updated to the database.');
}
return array(
$this
->view($identifier),
);
}