问题: ActiveRecord如何将单个类名与表名相关联?
我昨天才发现了ActiveRecord,很奇妙的php数据库框架。

但是,我仍然对以下工作感到困惑:
    

1.下面这个Person Model类 会自动将这个类指向到 people表

   

class Person extends ActiveRecord\Model {}

而我对类名和表名的联系的理解是下面这个关系

例如 Post.php

class Post extends ActiveRecord\Model {}

  这个Post Model 类的 话 是自动解析 Post类 指向的是posts表

所以问题就来了 !!!

Answer: 到底作者用了什么方法 把Person类 解析为了 people 表 而不是 persons表?

Question :

Your ActiveRecord\Model-derived class has a reference to an ActiveRecord\Table.

When the Table gets initialized (once per model-class through some static function calls), it is told it's model's classname.

Table::__construct($classname)

calls

Table::set_table_name()

Through the model's class name it asks if that class has statically overridden the table name. If not, it uses the Inflector library with:

Inflector::instance()->tableize

which is really just

StandardInflector::tableize($classname)

which underscorifies the name (Inflector::underscorify()) 
converts it to lower case (strtolower())
then hands it off to

Utils::pluralize()

  In the Utils library, you will find the pluralize and singularize implementations, which basically have some predefined plurals for the uncountable items (stuff that doesn't get pluralized like sheep and deer), some standard irregular forms (like child > children), and then some cool pluralization rules ($plural and $singular) that it runs through the regex parser.

#person会自动解析转义为people
#Utils.php private static $irregular = array(
'move' => 'moves',
'foot' => 'feet',
'goose' => 'geese',
'sex' => 'sexes',
'child' => 'children',
'man' => 'men',
'tooth' => 'teeth',
'person' => 'people'
);

  And remember you can override the defaults back in your model class with:

class MyModelClass extends ActiveRecord\Model {
static $table_name = 'whatever_it_is';
}

  

Thank you!

 
 

最新文章

  1. 【nginx配置】nginx做非80端口转发
  2. Bootstrap Metronic 学习记录(二)菜单栏
  3. Sweet Alert
  4. jQuery学习总结
  5. Hyper-V架构与VMware ESXi的差异
  6. 对于默认 Windows NT 安装的 SID 值
  7. EF Code First 导航属性 与外键
  8. Studio右键选项中没有Git?
  9. JAVA 图形界面 JFrame容器
  10. EventBroker
  11. Python 文件的IO
  12. centos 6.5网卡dhcp不能获得网关
  13. KMP算法之查找模式串在源串中出现的次数
  14. EJB开发第一个无状态会话bean、开发EJB客户端
  15. jq slideToggle()坑
  16. tensorflow 训练之tensorboard使用
  17. Parallel线程安全问题
  18. 【Math for ML】向量微积分(Vector Calculus)
  19. fiddle扩展
  20. Json&xml分析~

热门文章

  1. Java-1.6-并发包-集合
  2. openstack placement
  3. Error: 实例 "ddd" 执行所请求操作失败,实例处于错误状态。: 请稍后再试 [错误: Exceeded maximum number of retries. Exhausted all hosts available for retrying build failures for instance 6f60bc06-fcb6-4758-a46f-22120ca35a71.].
  4. spark sql 中的结构化数据
  5. [转]使用STM32CubeMX:USB大容量存储设备
  6. Django笔记(2)Json字段处理
  7. R-CNN 学习记录
  8. 服务发现 - consul 的介绍、部署和使用(转)
  9. 4.Python文件操作
  10. mac+windows下从git上拉取项目及运行