zoukankan      html  css  js  c++  java
  • PHP ActiveRecord demo栗子中 关于类名 的问题

    问题: ActiveRecord如何将单个类名与表名相关联?
    我昨天才发现了ActiveRecord,很奇妙的php数据库框架。
    
    但是,我仍然对以下工作感到困惑:
        

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

       

    class Person extends ActiveRecordModel {}

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

    例如 Post.php

    class Post extends ActiveRecordModel {}
    

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

    所以问题就来了 !!!

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

    Question :

    Your ActiveRecordModel-derived class has a reference to an ActiveRecordTable.

    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 ActiveRecordModel {
      static $table_name = 'whatever_it_is';
    }
    

      

    Thank you!

     
     
  • 相关阅读:
    关于python urlopen 一个类似radio流的timeout方法
    Python nltk English Detection
    Python依赖打包发布详细
    python 怎么和命令行交互
    Python中多维数组flatten的技巧
    Python中的url编码问题
    python数据持久存储:pickle模块的基本使用
    Python控制台输出不换行(进度条等)
    UnicodeEncodeError: 'ascii' codec can't encode character u'xe9' in position 7: ordinal not in range(128) [duplicate]
    json.loads(s) returns error message like this: ValueError: Invalid control character at: line 1 column 33 (char 33)
  • 原文地址:https://www.cnblogs.com/foreversun/p/6831013.html
Copyright © 2011-2022 走看看