lithium\data\source\Database::_updateFields()

protected method

Renders the fields part for update queries.

Will only include fields if they have been updated in the entity of the context. Also handles correct incremented/decremented fields.

Parameters

  • array $data
  • array $schema

    An array defining the schema of the fields used in the criteria.

  • object $context

    Generally a data\model\Query instance.

Returns

string|null

SQL fragment, with fields separated by comma. Null when the fields haven't been changed.

Source

	protected function _updateFields($data, $schema, $context) {
		$fields = [];
		$increment = [];

		if ($entity = $context->entity()) {
			$export = $entity->export();
			$increment = $export['increment'];

			array_map(function($key) use (&$data, $export){
				if (!empty($data[$key]) && $export['data'][$key] === $data[$key]) {
					unset($data[$key]);
				}
			}, array_keys($export['data']));

			if (!$data) {
				return null;
			}
		}

		foreach ($data as $field => $value) {
			$schema += [$field => ['default' => null]];
			$name = $this->name($field);

			if (isset($increment[$field])) {
				$fields[] = $name . ' = ' . $name . ' + ' . $this->value($increment[$field], $schema[$field]);
			} else {
				$fields[] = $name . ' = ' . $this->value($value, $schema[$field]);
			}
		}
		return join(', ', $fields);
	}