Wade Wilson Fri Jul 12 01:26:44 -0400 2013

Subject: How does ActiveRecord correlate the singular class name to the table name?

I only discovered ActiveRecord yesterday and its truly marvelous.

However, I'm still a bit confused by how the following works:

  1. your table name would be "people"
    class Person extends ActiveRecord\Model {}

So thats what i did with my table: posts.

class Post extends ActiveRecord\Model{}

At no point did I have to specify Post is actually equal to the table posts. So where/how does this magic happen?


Megan McVey Thu Jul 18 18:08:41 -0400 2013

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.




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:


which is really just


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


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.


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

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