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 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?