Skip to content

Commit

Permalink
Merge branch 'T10532-case-insensitive-column-map' of https://github.c…
Browse files Browse the repository at this point in the history
…om/niden/cphalcon into niden-T10532-case-insensitive-column-map

* 'T10532-case-insensitive-column-map' of https://github.com/niden/cphalcon:
  [#10532] - Corrected variable
  [#10532] - Updated changelog
  [#10532] - Added case insensitive column map in model
  • Loading branch information
niden committed Nov 4, 2018
2 parents 8518fb3 + 25dcfce commit 1491aa9
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
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;
}
}

0 comments on commit 1491aa9

Please sign in to comment.