public function RestfulDataProviderDbQuery::update in RESTful 7
Update an item based on the request object.
Parameters
mixed $id: The unique ID for the item.
boolean $full_replace: TRUE if the data on the request represents the new object to replace the existing one. FALSE if the request only contains the bits that need updating.
Return value
array The structured array for the item ready to be rendered.
Overrides RestfulBase::update
1 call to RestfulDataProviderDbQuery::update()
- RestfulDataProviderDbQuery::replace in plugins/
restful/ RestfulDataProviderDbQuery.php - Replace a record by another.
File
- plugins/
restful/ RestfulDataProviderDbQuery.php, line 387 - Contains \RestfulDataProviderDbQuery
Class
- RestfulDataProviderDbQuery
- @file Contains \RestfulDataProviderDbQuery
Code
public function update($id, $full_replace = FALSE) {
// Build the update array.
$request = $this
->getRequest();
static::cleanRequest($request);
$save = FALSE;
$original_request = $request;
$public_fields = $this
->getPublicFields();
$id_columns = $this
->getIdColumn();
$record = array();
foreach ($public_fields as $public_field_name => $info) {
// Ignore passthrough public fields.
if (!empty($info['create_or_update_passthrough'])) {
unset($original_request[$public_field_name]);
continue;
}
// If this is the primary field, skip.
if ($this
->isPrimaryField($info['property'])) {
continue;
}
if (isset($request[$public_field_name])) {
$record[$info['property']] = $request[$public_field_name];
}
elseif ($full_replace) {
$record[$info['property']] = NULL;
}
unset($original_request[$public_field_name]);
$save = TRUE;
}
// No request was sent.
if (!$save) {
throw new \RestfulBadRequestException('No values were sent with the request.');
}
// If the original request is not empty, then illegal values are present.
if (!empty($original_request)) {
$error_message = format_plural(count($original_request), 'Property @names is invalid.', 'Property @names are invalid.', array(
'@names' => implode(', ', array_keys($original_request)),
));
throw new \RestfulBadRequestException($error_message);
}
// Add the id column values into the record.
foreach ($this
->getIdColumn() as $index => $column) {
$record[$column] = current($this
->getColumnFromIds(array(
$id,
), $index));
}
// Once the record is built, write it.
if (!drupal_write_record($this
->getTableName(), $record, $id_columns)) {
throw new \RestfulServiceUnavailable('Record could not be updated to the database.');
}
// Clear the rendered cache before calling the view method.
$this
->clearRenderedCache(array(
'tb' => $this
->getTableName(),
'cl' => implode(',', $this
->getIdColumn()),
'id' => $id,
));
return $this
->view($id);
}