-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Automatic Conversion Between camelCase and snake_case #176
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Comments
The "conversion" is probably done by the getters of the Eloquent class, since this library does not change the getters, this behavior should still be there. Specifying the fields kind of goes against the "philosophy" of MongoDB that does not require a schema. |
You mention the getters, but not the setters: is the conversion being handled there as well? Thanks, Jens :) |
They are stored like the actual object attributes. I don't think Eloquent modifies the setters to snake or came case, or it would be the same in this library since this library completely extends the original Eloquent class. |
Ok, this is starting to make sense now. I think Eloquent will dynamically convert between camelCase and snake_case when saving, as the field name is already specified for RDBMS'. However, with MongoDB there is no schema to fall back on, so it inserts it the way the object property was created. Would this be worthy of a pull request? |
Interesting to note that the creation of collections is in snake_case (if not defined in the class), while creation of fields is however they are assigned in the collection object (like you said). I have not yet found a way to make Eloquent name the fields properly. Tried the following:
Perhaps there is a setting for this, or perhaps Eloquent just doesn't have this capability, relying on the existing schema? |
Perhaps this is what you're looking for:
Include it in your model that extends eloquent/moloquent. I use it to keep the names of my eloquent relationships camelCased when eager loading. |
Thank you for that tidbit! Didn't know it existed. :) Wish there was some documentation on all the database modifiers we can use in the models. Did find this discussion around the topic though: laravel/framework#391 (comment), and maybe it's not a huge deal -- I was just looking for consistency. I will try playing around with the snakeAttributes setting, and if that doesn't work, I will leave it be. :) Thanks! |
I am trying to put a proper convention into place for my current (new) project, and am struggling with worrying about casing for database naming conventions vs code naming conventions.
I believe Eloquent will automatically convert between camelCase for class attributes and snake_case for database field names. (Issue #50 seemed to touch on this.) I don't believe Laravel-MongoDB is doing this at the moment, as all my collections are being created using camelCase, with the exception of the system fields (created_at, etc.).
Then I started wondering if I should even concern myself with this, as the code in Laravel takes precedence over what's in the database, as that is abstracted so much already anyway (why should I concern myself, when I only see the class attributes?).
On the other hand, is there a way to do this? Similar to how we can specify the collection (table) name in the class, can we specify field names?
What are the current thoughts on this subject?
Thanks!
The text was updated successfully, but these errors were encountered: