-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
[NFR] \Phalcon\Mvc\Model::columnMap() - Auto-map database columns that are not defined (like aliases) #1780
Comments
@phalcon what do you think if instead of if (Z_TYPE_P(column_map) == IS_ARRAY) {
if (phalcon_array_isset(column_map, field)) {
PHALCON_OBS_NVAR(attribute_field);
phalcon_array_fetch(&attribute_field, column_map, field, PH_NOISY);
} else {
PHALCON_INIT_NVAR(exception_message);
PHALCON_CONCAT_SVS(exception_message, "Column '", field, "' isn't part of the column map");
PHALCON_THROW_EXCEPTION_ZVAL(phalcon_mvc_model_exception_ce, exception_message);
return;
}
} else {
PHALCON_CPY_WRT(attribute_field, field);
} we do something like if (Z_TYPE_P(column_map) == IS_ARRAY && phalcon_array_isset(column_map, field)) {
PHALCON_OBS_NVAR(attribute_field);
phalcon_array_fetch(&attribute_field, column_map, field, PH_NOISY);
} else {
PHALCON_CPY_WRT(attribute_field, field);
} ? |
I could see how some people may prefer the current behavior (or expect it to throw an exception), so perhaps checking/setting a default option (like columnMapOptional = false) when implementing this feature would be better going forward. |
I agree, we need some option to keep the original behavior as this is more strict and help the developer to find possible bugs. |
I agree that the behavior is nice for strict development, however it is a great risk for production environments. For example, updating a database schema by adding a new column should never break the production code (not even for the split second). The migrations or annotated approach for schema are partly a solution, but it would also be nice to turn this off. Or, at least consider throwing a specialized exception which can be cought and silently ignored. Right now model would throw Phalcon\Mvc\Model\Exception without any error-specific code, and even the message itself contains column name. So, the only solution for me right now is catching Phalcon\Mvc\Model\Exception and searching for "isn't part of the column map" - which seems hacky ... |
Any news on this? Possible implementation? I also would like to see this implemented. At times I only need to (re)map 1 or 2 columns (mostly having to do with adding '_id' to some column) (mainly to prevent clashing with some relationship alias). (Some elaboration on my methods: I prefer to name my (foreign key) columns (in the db) to what feels natural to me, and at times that is without e.g. '_id' added to them (which then in most cases would effectively lead to them having the same name as the table they're referring to).) It's tedious to have to add all columns in the columnMap array even if I don't want to remap them. |
Any plan for this? |
Any progress on this? |
👍 for all theses recommendations, I have the same problem and it would be nice if it could escape unimplemented columns in the columnMap method |
This is fixed in the 2.0.x branch (Phalcon 2.0.4), you have to change a global ORM setting to allow unexpected columns: \Phalcon\Mvc\Model::setup(['ignoreUnknownColumns' => true]); |
It seems that the "ignoreUnkownColumns" isn't working (anymore). |
@timbrand Create please separated issue with small script to reproduce |
+1 |
Summary
Adding columns to a database table causes exceptions if those added columns were not defined in \Phalcon\Mvc\Model::columnMap() prior. This makes working with a live/production database difficult. I feel that columnMap() should feel and act like column aliases.
Database table schema:
Model
Issue: Adding a column to database table
Results in:
Phalcon\Mvc\Model\Exception: Column "product_description" doesn't make part of the column map
New Feature Request:
For database table columns that are not defined in
columnMap()
, could those columns automatically be added to the columnMap() (as if there was no column alias)?_Suggested Solution_
In the following
\Phalcon\Mvc\Model
methods:assign()
,cloneResultMap()
,cloneResultMapHyrdate()
,setSnapshotData()
Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.
The text was updated successfully, but these errors were encountered: