Extending Propel in Symfony 1.4


Allow Table name prefixed with underscore

Replace the original symfony/lib/plugins/sfPropelPlugin/lib/addon/sfPropelDatabaseSchema.class.php:

  public function getChildren($hash)
  {
    foreach ($hash as $key => $value)
    {
      // ignore special children (starting with _)
      if ($key[0] == '_')
      {
        unset($hash[$key]);
      }
    }

    return $hash;
  }

with:

  public function getChildren($hash)
  {
    foreach ($hash as $key => $value)
    {
      // ignore special children (starting with _)
      if (in_array($key, array('_attributes', '_behaviors', '_propel_behaviors', '_inheritance', '_nestedSet', '_foreignKeys', '_indexes', '_uniques')))
      {
        unset($hash[$key]);
      }
    }

    return $hash;
  }

Now you can define a table name which is prefixed by underscore in your config/schema.yml:

connection:                      propel
defaultIdMethod:                 native
package:                         lib.model

classes:
  MyTable:
    tableName:                   _my_table
    columns:
      id:
      column1:                   { type: varchar, size: 30 }
      column2:                   { type: varchar, size: 255 }

Match the Propel Generated Model Files with Symfony Coding Standard

Propel 1.4 introduce behaviors named alternative_coding_standarts which can be used to alter Propel generated model files according to symfony. But, this behavior known not working and need to be pacthed. Locate symfony/lib/plugins/sfPropelPlugin/lib/vendor/propel-generator/classes/propel/engine/behavior/AlternativeCodingStandardsBehavior.php, and change:

		if($this->getParameter('brackets_newline') == 'true') {
			$filter['#^(/t*)/}/h(else|elseif|catch)(.*)/h/{$#m'] = "$1}
$1$2$3
$1{";
			$filter['#^(/t*)(/w.*)/h/{$#m'] = "$1$2
$1{";
		}

to:

		if($this->getParameter('brackets_newline') == 'true') {
			// class
			$filter['#^(\w.*)\h\{#m'] = "$1\n{";
			// line ending with {
			$filter['#^(\t+)(\w.*)\h\{#m'] = "$1$2\n$1{";
			// } something {
			$filter['#^(\t+)\}\h(else|elseif|catch)(.*)\h\{#m'] = "$1}\n$1$2$3\n$1{";
		}

Change the config/propel.ini to include alternative_coding_standarts behavior as default:

propel.behavior.default                        = symfony,symfony_i18n

to:

propel.behavior.default                        = symfony,symfony_i18n,alternative_coding_standards

And then rebuild your model files.

Adding Behavior to Add __toString() Method for Model Files

To be able to automatically generate __toString() method, symfony_tostring behavior need to be added as default behavior or registered as symfony behaviors in config/schema.yml.

propel.behavior.default                        = symfony,symfony_i18n,symfony_tostring,alternative_coding_standards
propel.behavior.symfony_tostring.class         = plugins.sfPropelPlugin.lib.behavior.SfPropelBehaviorToString

To include a column in __toString() method, define an attribute toString: true to column definition, as in:

classes:
  MyTable:
    tableName:                   _my_table
    columns:
      id:
      column1:                   { type: varchar, size: 30, toString: true }
      column2:                   { type: varchar, size: 255 }

This behavior accept an argument named separator which will be used to concatenate the column if there are many columns defined with toString attribute.

classes:
  MyTable:
    tableName:                   _my_table
    columns:
      id:
      column1:                   { type: varchar, size: 30, toString: true }
      column2:                   { type: varchar, size: 255, toString: true }
    behaviors:
      symfony_tostring:
        separator:               ' - '

Dont forget to rebuild your models.

Download: http://numpang.bkdjombang.com/files/symfony/1.4/extending-propel-in-symfony-1.4.zip

Advertisements

  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: