Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

T10532 case insensitive column map #13569

Merged
merged 4 commits into from
Nov 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG-4.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
- Added `Phalcon\Acl\Adapter\Memory::addRole` support multiple inherited
- Added `Phalcon\Tag::renderTitle()` that renders the title enclosed in `<title>` tags. [#13547](https://github.com/phalcon/cphalcon/issues/13547)
- Added `hasHeader()` method to `Phalcon\Http\Response` to provide the ability to check if a header exists. [PR-12189](https://github.com/phalcon/cphalcon/pull/12189)
- Added global setting `orm.case_insensitive_column_map` to attempt to find value in the column map case-insensitively. Can be also enabled by setting `caseInsensitiveColumnMap` key in `\Phalcon\Mvc\Model::setup()`. [#11802](https://github.com/phalcon/cphalcon/pull/11802)

## Changed
- By configuring `prefix` and `statsKey` the `Phalcon\Cache\Backend\Redis::queryKeys` no longer returns prefixed keys, now it returns original keys without prefix. [PR-13456](https://github.com/phalcon/cphalcon/pull/13456)
Expand Down
4 changes: 4 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@
"orm.disable_assign_setters": {
"type": "bool",
"default": false
},
"orm.case_insensitive_column_map": {
"type": "bool",
"default": false
}
},
"destructors": {
Expand Down
42 changes: 41 additions & 1 deletion phalcon/mvc/model.zep
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,11 @@ abstract class Model implements EntityInterface, ModelInterface, ResultInterface

for attribute in metaData->getAttributes(this) {

// Try to find case-insensitive key variant
if !isset columnMap[attribute] && globals_get("orm.case_insensitive_column_map") {
let attribute = self::caseInsensitiveColumnMap(columnMap, attribute);
}

// Check if we need to rename the field
if typeof columnMap == "array" {
if !fetch attributeField, columnMap[attribute] {
Expand Down Expand Up @@ -784,6 +789,11 @@ abstract class Model implements EntityInterface, ModelInterface, ResultInterface

if typeof columnMap == "array" {

// Try to find case-insensitive key variant
if !isset columnMap[key] && globals_get("orm.case_insensitive_column_map") {
let key = self::caseInsensitiveColumnMap(columnMap, key);
}

/**
* Every field must be part of the column map
*/
Expand Down Expand Up @@ -2307,6 +2317,11 @@ abstract class Model implements EntityInterface, ModelInterface, ResultInterface
continue;
}

// Try to find case-insensitive key variant
if !isset columnMap[key] && globals_get("orm.case_insensitive_column_map") {
let key = self::caseInsensitiveColumnMap(columnMap, key);
}

/**
* Every field must be part of the column map
*/
Expand Down Expand Up @@ -2391,7 +2406,7 @@ abstract class Model implements EntityInterface, ModelInterface, ResultInterface
var disableEvents, columnRenaming, notNullValidations,
exceptionOnFailedSave, phqlLiterals, virtualForeignKeys,
lateStateBinding, castOnHydrate, ignoreUnknownColumns,
updateSnapshotOnSave, disableAssignSetters;
updateSnapshotOnSave, disableAssignSetters, caseInsensitiveColumnMap;

/**
* Enables/Disables globally the internal events
Expand Down Expand Up @@ -2456,6 +2471,10 @@ abstract class Model implements EntityInterface, ModelInterface, ResultInterface
globals_set("orm.ignore_unknown_columns", ignoreUnknownColumns);
}

if fetch caseInsensitiveColumnMap, options["caseInsensitiveColumnMap"] {
globals_set("orm.case_insensitive_column_map", caseInsensitiveColumnMap);
}

if fetch updateSnapshotOnSave, options["updateSnapshotOnSave"] {
globals_set("orm.update_snapshot_on_save", updateSnapshotOnSave);
}
Expand Down Expand Up @@ -2540,6 +2559,11 @@ abstract class Model implements EntityInterface, ModelInterface, ResultInterface
* Check if the columns must be renamed
*/
if typeof columnMap == "array" {
// Try to find case-insensitive key variant
if !isset columnMap[attribute] && globals_get("orm.case_insensitive_column_map") {
let attribute = self::caseInsensitiveColumnMap(columnMap, attribute);
}

if !fetch attributeField, columnMap[attribute] {
if !globals_get("orm.ignore_unknown_columns") {
throw new Exception("Column '" . attribute . "' doesn't make part of the column map");
Expand Down Expand Up @@ -4802,4 +4826,20 @@ abstract class Model implements EntityInterface, ModelInterface, ResultInterface
{
return count(this->_errorMessages) > 0;
}

/**
* Attempts to find key case-insensitively
*/
private static function caseInsensitiveColumnMap(var columnMap, var key) -> string
{
var cmKey;

for cmKey in array_keys(columnMap) {
if strtolower(cmKey) == strtolower(key) {
return cmKey;
}
}

return key;
}
}