PDO + Angular:如何协调 DB underscore_case 与 Angular 的驼峰命名法?

PDO + Angular: how to reconcile DB underscore_case with Angular's camelCase?

我有一个 AngularJs 应用程序,它使用 php PDO API 连接到数据库。

这是数据流示意图:

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
  });

问题是 SQL 推荐的命名标准是 underscore_case,而 Angular 的是驼峰命名...

我不想禁用 jslint,它在其他方面非常有用...

我知道即使

我也可以避免警告
{
  "camelcase": false
}

在 .jshintrc 中,但我不希望全局禁用驼峰式检查...

目前我用 "jslint comments" 避免 jslint 警告,这样:

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

但我的代码将包含比代码更多的 jslint 注释...完全不可读...

你(或你会)如何解决这个问题?

像使用数组一样使用对象:person['first_name']

因为 underscore_case 对于 SQL 不是强制性的,而对于 Angular 驼峰式是强制性的,所以对这两种环境都使用名称 suitable 是明智的。在这种情况下,camelCase 是 acceptable 和 both.

在 40 多年的编程生涯中,我从未认为 underscore_case 有用。我一直喜欢驼峰命名法。将 first_name 重命名为 firstName 不仅看起来更好,而且可以解决问题。

编辑

标准 SQL 中的列名可以由字母、下划线和 以字母开头的数字。 SQL 允许您在名称中使用空格、保留字和特殊字符,前提是您将它​​们括在双引号中。

标识符不仅要有意义,而且必须是唯一的。因此,在选择命名约定时,必须考虑操作环境

Windows

SQL 不区分大小写,即 NAME == name == Name == NaMe

Unix

SQL 区分大小写,即 NAME != name != Name != NaMe

Michael Lato 为 MS Server 推荐 PascalCase, Oracle 使用带下划线的 UPPER。 MySQL 建议使用下划线,但这不是强制性的 See

注意

在 Windows 上,InnoDB 始终以小写形式在内部存储数据库和 table 名称