Name Generation

As the Propel generator creates DDL and PHP code, it has the need to convert names and in some cases create unique names. Here we'll look at how Propel generates PHP names and how it generates constraint names -- and how you can customize the generation of both.

PHP Name Generation

For most users, the most significant aspect of Propel's name generation is in the transformation of the identifier names that you specify in the schema.xml to the names that are used for PHP constants and methods.

The Default Behavior

Propel does not transform the class constants and instance variables:

  • The constant names that Propel generates are always all-uppercase versions of the identifier (table/column) name specified in schema.xml. As we'll see later, though, you can specify explicit values for these constants.
  • The protected instance variables are just exact copies of the identifier (table/column) name specified in schema.xml.

By default, Propel wants to generate CamelCase method names and will use the "underscore method" of translating these names. This means that first character is capitalized as is every character after an underscore. Perhaps best illustrated with an example:

Identifier name in schema.xmlCorresponding class/method nameUsage Example
my_tableMyTablenew MyTable()
another_column_nameAnotherColumnName$obj->getAnotherColumnName()

Customizing Naming

There are a number of situations where the default naming may not be appropriate. Here are a couple examples: * You use plural names for your database tables but don't like single objects looking "plural" in Propel (e.g. $joe = new Users()). * You use mixed-case names in your database but would prefer to have more traditional (e.g. CamelCase) names in your generated model code. * You want to prefix your classes. * ... I'm sure there are many more you could add to the list.

There are two strategies for customizing the names that Propel uses for classes & methods. 1. Specify custom names 1. Change the name generator that is being used

Specifying Custom PHP Names

The most common approach is to simply specify custom PHP names for the tables or columns. This is typically accomplished using the phpName attribute.

For example, to change the name of a class that is generated for a table:

<table name="account_balances" phpName="AccountBalance">
  <!-- -->
</table>

For columns, you can customize the name used in methods (getters, setters, join methods, etc.) and also the name that is used for the generated constant (via the peerName attribute). You may need to change the constant name, for example, if you are using a PHP reserved word as your column name ("class" would be a good example).

<table name="enrollment" phpName="EnrollmentRecord">
  <column name="studentname" phpName="StudentName" type="varchar"/>
  <column name="class" peerName="CLAZZ" type="varchar"/>
</table>

Changing the Name Generator

You can also change the name generator that is used. Propel supports three (3) name generators: 1. underscore (the default) 1. phpname - Similar to "underscore", but nothing is converted to lowercase. 1. nochange - The name is not changed.

You can customize the name generation at the database level (so for all tables) or for a specific table.

For example (database-level):

<database name="bookstore" defaultPhpNamingMethod="nochange">
  <!-- -->
</database>

or (table-level):

<table name="book" phpNamingMethod="nochange">
  <!-- -->
</table>

Constraint Name Generation

Propel also generates names for other database identifiers -- such as foreign key constraints, indexes, unique indexes. Propel generates names that are unique (at least unique in the context of your schema.xml) and it takes into consideration factors like the maximum name length for the database your are building against.

This is not as customizable as the PHP name generation, but in general you can specify your own names for anything that Propel would otherwise generate.

For example, you can choose to specify your own name for your foreign keys or you can leave it up to Propel to generate one for you.

!#xml
<foreign-key name="my_fk" ....>