mongodb / laravel-mongodb

A MongoDB based Eloquent model and Query builder for Laravel (Moloquent)
https://www.mongodb.com/compatibility/mongodb-laravel-integration
MIT License
7k stars 1.43k forks source link

Automatic Conversion Between camelCase and snake_case #176

Closed mikebronner closed 10 years ago

mikebronner commented 10 years ago

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!

jenssegers commented 10 years ago

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.

mikebronner commented 10 years ago

You mention the getters, but not the setters: is the conversion being handled there as well? How are your database fields stored (camel or snake)?

Thanks, Jens :)

jenssegers commented 10 years ago

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.

mikebronner commented 10 years ago

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?

mikebronner commented 10 years ago

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?

khamkham commented 10 years ago

Perhaps this is what you're looking for:

public static $snakeAttributes = false;

Include it in your model that extends eloquent/moloquent. I use it to keep the names of my eloquent relationships camelCased when eager loading.

mikebronner commented 10 years ago

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: https://github.com/laravel/framework/issues/391#issuecomment-13844569, 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! ~Mike