dongyu4554 2015-04-26 08:21
浏览 51
已采纳

PDO + Angular:如何使用Angular的camelCase协调DB underscore_case?

I have an AngularJs app which connects to a database using php PDO APIs.

This is the schematic data flow:

DB:
  CREATE TABLE person (
    ...
    first_name,
    ...
  );

php API:
  $stmt = $this->db->prepare("SELECT * FROM person");
  $stmt->execute();
  $result = $stmt->fetchAll(PDO::FETCH_ASSOC);
  var_dump($result);
  ...
  'first_name' => 'Alice',
  ...

angular service:
  getPerson: function (id) {
    return $http({
      method: 'GET',
      url: apiUri + 'get' + '/' + id,
    }).then(handleSuccess, handleError);
  },

angular controller:
  Person.getPerson(id).then(function(person) {
    var name = person.first_name; // this throws a jslint warning
  });

The problem is SQL recommended naming standard is underscore_case, while Angular's is camelCase...

I would not like to disable jslint, which is very useful, in other respects...

I know I could avoid warnings even with

{
  "camelcase": false
}

in .jshintrc, but I'd prefer not to disable camelcase check globally...

Currently I avoid jslint warnings with "jslint comments", this way:

/* jshint camelcase: false */
var name = person.first_name;
/* jshint camelcase: true */

but my code is going to contain more jslint comments than code... Quite unreadable...

How do you (or would you) solve this issue?

  • 写回答

2条回答 默认 最新

  • duanjuelu8874 2015-04-26 10:57
    关注

    As underscore_case is not mandatory with SQL and camelCase is mandatory with Angular it is sesible to use names suitable for both environments. In this case camelCase is acceptable with both.

    In 40+ years programming I never thought underscore_case was useful. I have always preferred camelCase. Rename first_name to firstName it not only looks better but it will solve the problem.

    EDIT

    Column names in Standard SQL can consist of letters, underscores, and digits starting with a letter. SQL allows you to use spaces, reserved words, and special characters in a name if you enclose them in double quotation marks.

    Identifiers not only have to be meaningful they have to be unique. So in choosing a naming convention the environment in which the operate has to be taken into consideration

    Windows

    SQL is case insesitive ie, NAME == name == Name == NaMe

    Unix

    SQL is case sesitive ie, NAME != name != Name != NaMe

    Michael Lato recomends PascalCase for MS Server, Oracle uses UPPER with underscores. MySQL recomends lower with underscores BUT this is not mandatory See

    NOTE

    On Windows, InnoDB always stores database and table names internally in lowercase

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题