PHP ActiveRecord demo栗子中 關於類名 的問題

來源:互聯網
上載者:User

標籤:err   tee   dem   call   can   關聯   highlight   工作   翻譯   

問題: 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.phpprivate 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!

 

 

 

 

 
 

PHP ActiveRecord demo栗子中 關於類名 的問題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.