Exporting beautiful column names as camel case

350 views
Skip to first unread message

db82

unread,
Nov 5, 2013, 1:39:09 PM11/5/13
to redbe...@googlegroups.com
My backbone js model names are in camel case, and by db columns are underscored.

When saving rows, the camels are converted to underscores.

But when pulling and exporting, there are no camels to be seen (in the desert). All the columns are exported with underscores and hence my backbone models cannot find their attributes.

Is there any way to have underscore to camel conversion on export / exportAll ?

gabor

unread,
Nov 5, 2013, 1:51:31 PM11/5/13
to redbe...@googlegroups.com


Hi,

Export reveals the true column names. These are *always* snake_cased because uppercase characters cause various compatibility issues across different platforms (even within the same database, for instance MySQL+Windows).

I recommend to use a little plugin to convert snake_case to camelCase.
Should be available somewhere...

cheers,
Gabor

Zewa One

unread,
Nov 5, 2013, 2:16:41 PM11/5/13
to redbe...@googlegroups.com
Here a small example how you could do it:

$user = R::dispense('user');
    $user->name_column = "name";
    $user->title_column = "title";
    $user->ownHobby = $hobby;

    $hobby = R::dispense('hobby');
    $hobby->name = "PHP";

    R::store($user);

    $exported = R::exportAll($user);

    function to_camel_case($str, $capitalise_first_char = false) {
        if($capitalise_first_char) {
          $str[0] = strtoupper($str[0]);
        }
        $func = create_function('$c', 'return strtoupper($c[1]);');
        return preg_replace_callback('/_([a-z])/', $func, $str);
    }

    $renamed = array_map(function($bean) {
        $arr = array();
        foreach($bean as $key => $value)
        {
            $arr[to_camel_case($key)] = $value;
        }
        return $arr;
    }, $exported);
    var_dump($renamed);

gabor

unread,
Nov 5, 2013, 5:22:25 PM11/5/13
to redbe...@googlegroups.com

I feel another plugin coming... :-P

db82

unread,
Nov 5, 2013, 7:39:38 PM11/5/13
to redbe...@googlegroups.com
Please tell!

Zewa One

unread,
Nov 6, 2013, 2:50:00 AM11/6/13
to redbe...@googlegroups.com
@db82
did that help or you need something else?

@gabor
this time I'd even vote for putting this maybe as an enhancement directly into the core.
What do you think about that?

e.g.
R::exportAll($beans, RedBean::EXPORT_AS_CAMELCASE);

Greetz
Zewa

db82

unread,
Nov 6, 2013, 6:51:52 AM11/6/13
to redbe...@googlegroups.com
@Zewa
Thanks for your help, I've not had a chance to try it yet but I'm sure it's doable with your code so many thanks.

@gabor
From my point of view, if column names are formatted when setting, there really needs to be an option for formatting them back when getting.

db82

unread,
Nov 6, 2013, 12:28:37 PM11/6/13
to redbe...@googlegroups.com
@Zewa Worked a treat, thanks!

gabor

unread,
Nov 7, 2013, 3:57:24 PM11/7/13
to redbe...@googlegroups.com

Sounds reasonable, I will implement this.

gabor

unread,
Nov 7, 2013, 4:00:10 PM11/7/13
to redbe...@googlegroups.com


Any ideas for method signature?

 exportAll( $beans, $parents = FALSE, $filters = array() )

cannot use second param for this.... :(

Zewa One

unread,
Nov 8, 2013, 4:21:41 AM11/8/13
to redbe...@googlegroups.com
Is refactoring the method to following signature a viable solution?

exportAll( $beans, $settings = array(), $filters = array() )

and then move the parents = false inside the settings array.

I really do not like the overloading mechanisms with the magic _call method of PHP
and since none of the standard overloading methods like in other languages exist I'd recommend the refactoring.

Greetz
Zewa

gabor

unread,
Nov 8, 2013, 4:57:55 PM11/8/13
to redbe...@googlegroups.com
that would break backward compatibility...

indiekiduk

unread,
Nov 8, 2013, 6:32:56 PM11/8/13
to redbe...@googlegroups.com
FYI create_function in a loop leaks.

In comparing that to_camel_case function to Redbean's beau function for from camel, I discovered a potential problem in how would you go from one_acl_route -> oneACLRoute ?

What if we could put the camel version of the property in a meta field and then any to array or export functions would use that name instead?

gabor

unread,
Nov 9, 2013, 9:27:52 AM11/9/13
to redbe...@googlegroups.com


You're right we miss information, we can't get that back.
This feature cannot be implemented.

Zewa One

unread,
Nov 9, 2013, 12:15:37 PM11/9/13
to redbe...@googlegroups.com
@backward compatibility
yeah agree thats a sad thing ... as said _call overrides would still allow to keep both versions, thou I find the solution a little bit hacky.

@create_function
Thx for the hint, didnt know that.
The above implementation of the function was actually just copied from a quick SO search, so didn't care so far to inspect performance.
Anyways will keep that in mind for future work :)

@one_acl_route -> oneACLRoute
Well I wouldn't call that exactly a nice camelCasedAppearence. I'd personally always go with only the first letter capitalized no matter if word or abbreviation.
the reason is that you do not clearly see on a fast look where the ABBR stops and the next word starts (one ... ACLR ... oute)
But I have to agree there are definitely X different ideas what is right and wrong :) 
Here is a nice post talking about problems with CC and why quite a lot guys like to stay with underscored version 

To be open for all possible cases, the easiest would be to pass an array where you map the original column name to your desired output.

E.g.
exportAll( $beans, false , null, array( 'my_column_name' => 'MyColumnName', 'my_acl_route' => 'myACLRoute' ) )
Reply all
Reply to author
Forward
0 new messages