Underscore naming convention is popular standard for table and column naming. Generated AR attributes use underscore naming convention too. It's contrary framework code style, contrary corporative code style.
To avoid mistakes, names conversion will work only for ActiveRecord attributes. Names conversion is very simple action does not affect performance. Conversion turn on by switcher, by default off.
It's not as simple as it seems. Especially when it comes to queries and the fact that you should use field_name in conditions and fieldName when accessing properties.
Yes, therefore It only for attributes: insert, update, access to attributes.
we have discussed this before and it is confusing if attributes are named different in insert, update than in query.
see #6760, #7136
It's not required feature. This will be useful someone.
Omg this is really annoying and kinda a deal breaker for us.... Why not just give an option to use camelCase ?
This greatly increases code consistency, Hundreds of devs wish for this feature and your reason to not do it is because it is "confusing"?
Thinking of switching to Laravel because of this or enhancing ur implementation....
Kinda disappointed
@j3rrey it's not as simple as it looks as I've already pointed out https://github.com/yiisoft/yii2/issues/9409#issuecomment-130868475.
If you have a plan for a complete solution which isn't too complex and solves the two-way mapping, it would be great.
I will give it a shot and come back later to this issue, and yes I've read all the issues and your answers
Looking forward to it.
Thinking of switching to Laravel because of this or enhancing ur implementation....
I am not aware of laravel supporting this feature, can you point me to the docs or implementation?
Hey hey, so I don't have the time to enhance your current implementation.... also your comment "it's confusing" kinda was a deal breaker to further use your framework.
I guess we will switch if there is no easy solution.
@cebe It's all here in the eloquent documentation https://laravel.com/docs/5.4/eloquent
https://en.wikipedia.org/wiki/Adapter_pattern
It's all here in the eloquent documentation https://laravel.com/docs/5.4/eloquent
Where do you see automatic conversion of attributes names in eloquent? Even in docs they use snake_case for attributes names:
$user = App\User::find(1);
$firstName = $user->first_name;
I also can not find it. I'm not even sure if this is supported at all in current Laravel version, see some old tickets:
Well yes, there is naming strategy there.
Just tried this and it works for my code:
found it here:
https://laracasts.com/discuss/channels/eloquent/eloquent-accessor
class YourModel extends Model
{
public static $snakeAttributes = false;
}
defined in /Illuminate/Database/Eloquent/Model
/**
* Indicates whether attributes are snake cased on arrays.
*
* @var bool
*/
public static $snakeAttributes = false;
There should also be another way, since when defining relations you can also define column names.
Anyways if all of the above fails for some version there is still this 3rd Party Package for Laravel:
https://github.com/jarektkaczyk/eloquence/wiki/Mappable
https://github.com/jarektkaczyk/eloquence
I'm sure it was somewhere in the documentation maybe a bit hidden.
In this issue Taylor said he is fine with it, not sure if he only meant the created_at and updated_at attributes. https://github.com/laravel/framework/issues/316
Checking the actual code later today:
https://laravel.com/api/5.4/Illuminate/Database/Eloquent/Model.html
https://github.com/laravel/framework/blob/5.4/src/Illuminate/Database/Eloquent/Model.php
This can be realized by gii generating. Add getters/setters in template, class body.
<?php foreach ($tableSchema->columns as $column): ?>
<?php
$camelCaseName = Inflector::camelize($column->name);
$variableName = Inflector::variablize($column->name); ?>
/**
* @return <?= $column->phpType . "\n"; ?>
*/
public function get<?= $camelCaseName; ?>()
{
return $this->getAttribute('<?= $column->name; ?>');
}
/**
* @param <?= $column->phpType . ' $' . $variableName. "\n"; ?>
*/
public function set<?= $camelCaseName; ?>($<?= $variableName; ?>)
{
$this->setAttribute('<?= $column->name; ?>', $<?= $variableName; ?>);
}
<?php endforeach; ?>
@zetamen Also you can add
@property $columnNameCamelCase
at the class definition for autocomplete on IDEs
So camel naming is supported now? Thanks!~
Most helpful comment
This can be realized by gii generating. Add getters/setters in template, class body.