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

ORM: Case-insensitive column map option #11802

Closed
wants to merge 4 commits into from
Closed
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.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
- Using a settable variable for the Mongo Connection Service name instead of a hard coded string [#11725](https://github.com/phalcon/cphalcon/issues/11725)
- Added new getter `Phalcon\Mvc\Model\Query\Builder::getJoins()` - to get join parts from query builder
- Fixed `Phalcon\Db\Dialect\Oracle::prepareTable()` to correctly generate SQL for table aliases [#11799](https://github.com/phalcon/cphalcon/issues/11799)
- Added global setting `orm.try_ci_column_map` to attempt to find value in the column map case-insensitively if it is not found, what is a real fix for many possible bugs with Oracle column capitalization. Can be also enabled by setting `tryCIColumnMap` key in `\Phalcon\Mvc\Model::setup()`.

# [2.0.11](https://github.com/phalcon/cphalcon/releases/tag/phalcon-v2.0.11) (????-??-??)
- Fix Model magic set functionality to maintain variable visibility and utilize setter methods.[#11286](https://github.com/phalcon/cphalcon/issues/11286)
Expand Down
4 changes: 4 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@
"orm.ignore_unknown_columns": {
"type": "bool",
"default": false
},
"orm.try_ci_column_map": {
"type": "bool",
"default": false
}
},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix please indents

"destructors": {
Expand Down
45 changes: 44 additions & 1 deletion phalcon/mvc/model.zep
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,20 @@ abstract class Model implements EntityInterface, ModelInterface, ResultInterface
return this;
}

/**
* Attempts to find key case-insensitively
*/
private static function columnMapCIResolve(var columnMap, var key) -> string
{
var cmKey;
for cmKey in array_keys(columnMap) {
if strtolower(cmKey) == strtolower(key) {
return cmKey;
}
}
return key;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For indents we use tabs in zep files instead of spaces

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sergeyklay I think in zep diffs the tabs are there already :)


/**
* Assigns values to a model from an array returning a new model.
*
Expand Down Expand Up @@ -530,6 +544,11 @@ abstract class Model implements EntityInterface, ModelInterface, ResultInterface
continue;
}

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

// Every field must be part of the column map
if !fetch attribute, columnMap[key] {
if !globals_get("orm.ignore_unknown_columns") {
Expand Down Expand Up @@ -638,6 +657,11 @@ abstract class Model implements EntityInterface, ModelInterface, ResultInterface
if typeof key == "string" {
if typeof columnMap == "array" {

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

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

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

/**
* Every field must be part of the column map
*/
Expand Down Expand Up @@ -4406,6 +4435,12 @@ abstract class Model implements EntityInterface, ModelInterface, ResultInterface
* Check if the columns must be renamed
*/
if typeof columnMap == "array" {

// Try to find case-insensitive attribute variant
if !isset columnMap[attribute] && globals_get("orm.try_ci_column_map") {
let attribute = self::columnMapCIResolve(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 @@ -4454,7 +4489,8 @@ abstract class Model implements EntityInterface, ModelInterface, ResultInterface
{
var disableEvents, columnRenaming, notNullValidations,
exceptionOnFailedSave, phqlLiterals, virtualForeignKeys,
lateStateBinding, castOnHydrate, ignoreUnknownColumns;
lateStateBinding, castOnHydrate, ignoreUnknownColumns,
tryCIColumnMap;

/**
* Enables/Disables globally the internal events
Expand Down Expand Up @@ -4518,6 +4554,13 @@ abstract class Model implements EntityInterface, ModelInterface, ResultInterface
if fetch ignoreUnknownColumns, options["ignoreUnknownColumns"] {
globals_set("orm.ignore_unknown_columns", ignoreUnknownColumns);
}

/**
* Allows to try to find attributes in column map case-insensitively (needed for Oracle)
*/
if fetch tryCIColumnMap, options["tryCIColumnMap"] {
globals_set("orm.try_ci_column_map", tryCIColumnMap);
}
}

/**
Expand Down