Skip to content

Instantly share code, notes, and snippets.

@kriswallsmith
Created October 23, 2009 15:39
Show Gist options
  • Save kriswallsmith/216983 to your computer and use it in GitHub Desktop.
Save kriswallsmith/216983 to your computer and use it in GitHub Desktop.
<?php
protected function addDefaultBehaviors(SimpleXMLElement $table, $behaviors = array(), Properties $buildProperties)
{
$behaviors = array_merge($behaviors, $this->getBehaviors($table));
if ($buildProperties->getProperty('propel.builder.addBehaviors') && !in_array('symfony_behaviors', $behaviors))
{
$this->addBehavior($table, 'symfony_behaviors');
}
if (!in_array('symfony_extra', $behaviors))
{
$this->addBehavior($table, 'symfony_extra');
}
if (!in_array('symfony_i18n', $behaviors) && 'true' == $table['isI18N'])
{
$this->addBehavior($table, 'symfony_i18n', array(
'i18n_table' => (string) $table['i18nTable'],
));
}
if (!in_array('symfony_timestampable', $behaviors))
{
$this->addTimestampableBehavior($table);
}
}
protected function addTimestampableBehavior(SimpleXMLElement $table)
{
$parameters = array();
foreach ($table->column as $column)
{
$name = (string) $column['name'];
if (!isset($parameters['created_column']) && in_array($name, array('created_at', 'created_on')))
{
$parameters['created_column'] = $name;
}
if (!isset($parameters['updated_column']) && in_array($name, array('updated_at', 'updated_on')))
{
$parameters['updated_column'] = $name;
}
}
if ($parameters)
{
$this->addBehavior($table, 'symfony_timestampable', $parameters);
}
}
protected function addBehavior(SimpleXMLElement $table, $name, $parameters = array())
{
$this->logSection('behavior+', sprintf('%s: %s', $table['name'], $name));
$behavior = $table->addChild('behavior');
$behavior['name'] = $name;
foreach ($parameters as $key => $value)
{
$parameter = $behavior->addChild('parameter');
$parameter['name'] = $key;
$parameter['value'] = $value;
}
}
protected function getBehaviors(SimpleXMLElement $node)
{
$behaviors = array();
foreach ($node->behavior as $behavior)
{
$behaviors[] = (string) $behavior['name'];
}
return $behaviors;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment