Forked from anonymous/gist:e8a986aba73db0375ec91c7cc7411839
Last active
October 18, 2017 02:08
-
-
Save destinydriven/6d95a5385775352ed2c1f173a44fe97d to your computer and use it in GitHub Desktop.
_prepareUpdateFields()
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Quotes and prepares fields and values for an SQL UPDATE statement | |
* | |
* @param Model $Model The model to prepare fields for. | |
* @param array $fields The fields to update. | |
* @param bool $quoteValues If values should be quoted, or treated as SQL snippets | |
* @param bool $alias Include the model alias in the field name | |
* @return array Fields and values, quoted and prepared | |
*/ | |
protected function _prepareUpdateFields(Model $Model, $fields, $quoteValues = true, $alias = false) { | |
$quotedAlias = $this->startQuote . $Model->alias . $this->endQuote; | |
$schema = $Model->schema(); | |
$updates = array(); | |
foreach ($fields as $field => $value) { | |
if ($alias && strpos($field, '.') === false) { | |
$quoted = $Model->escapeField($field); | |
} elseif (!$alias && strpos($field, '.') !== false) { | |
$quoted = $this->name(str_replace($quotedAlias . '.', '', str_replace( | |
$Model->alias . '.', '', $field | |
))); | |
} else { | |
$quoted = $this->name($field); | |
} | |
if ($value === null) { | |
$updates[] = $quoted . ' = NULL'; | |
continue; | |
} | |
$update = $quoted . ' = '; | |
if ($quoteValues) { | |
$update .= $this->value($value, $Model->getColumnType($field), isset($schema[$field]) ? $schema[$field]['null'] : true); | |
} elseif ($Model->getColumnType($field) === 'boolean' && (is_int($value) || is_bool($value))) { | |
$update .= $this->boolean($value, true); | |
} elseif (!$alias) { | |
$update .= str_replace($quotedAlias . '.', '', str_replace( | |
$Model->alias . '.', '', $value | |
)); | |
} | |
elseif (is_string($value)) { | |
$update .= $this->value($value, 'string'); | |
} else { | |
$update .= $value; | |
} | |
$updates[] = $update; | |
} | |
return $updates; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment