Search code examples
mysqlpropel

Propel 1.7 double pluralization of the method names used for accessing related objects


I am using (for the first time) propel 1.7 and the propel-gen shell script provided with the installation. The schema.xml is generated from an existing database with propel-gen reverse, after that the classes are generated with propel-gen om.

My database structure is built in a such manner that it provides translations for various data in a separate table, with a one to many relationship. This is an example:

TABLE 'buildings'
  'id' int(10) unsigned NOT NULL AUTO_INCREMENT,
  'address' varchar(255) NOT NULL,

TABLE 'building_description_translations'
  'building_id' int(10) unsigned NOT NULL,
  'lang' varchar(2) NOT NULL,
  'description' varchar(1023) NOT NULL,

building_description_translations.building_id has a foreign key to buildings.id. Also, building_description_translations's primary key is over the building_id and lang columns.

So far so great, the propel generator works like a charm, except that the method names used to access the related objects from relationships end up being pluralized twice:

// file build/classes/myproject/om/BaseBuildings
class BaseBuildings{
    //...

    // note the TranslationSS
    public function getBuildingDescriptionTranslationss($criteria = null, PropelPDO $con = null)
    {
    //...
    }   

}

Also, my build.properties file looks like (besides the database configuration):

propel.builder.pluralizer.class = builder.util.StandardEnglishPluralizer
propel.samePhpName = true
propel.addVendorInfo = true

Is there a way to control this double-pluralization, without interfering with the existing database schema? I've already searched for an answer, but I couldn't find anything relevant to this scenario.


Solution

  • I figured it out - the easiest way is to create a custom Pluralizer class which will suit my needs, based on the original one which is located at generator\lib\builder\util\StandardEnglishPluralizer.php

    Let's say the new Pluralizer is

    generator\lib\builder\util\MyEnglishPluralizer.php

    In build.properties, the pluralizer configuration needs to be chaged accordingly.

    propel.builder.pluralizer.class = builder.util.MyEnglishPluralizer

    And then, regenerate the classes using propel-gen om

    In my case, I only had to add an entry to the $_plural property

    protected $_plural = array(
            // ...
            'ons' => 'ons'
        );
    

    Note: I've used the propel-gen script provided with the propel installation, and not the one from the PEAR package.