Skip to content

Instantly share code, notes, and snippets.

@bes89
Created July 14, 2013 17:55
Show Gist options
  • Save bes89/5995093 to your computer and use it in GitHub Desktop.
Save bes89/5995093 to your computer and use it in GitHub Desktop.
Doctrine2: Generate correct variable names (singular) in add or remove methods
<?php
// File: doctrine/orm/lib/Doctrine/ORM/Tools/EntityGenerator.php
// ...
class EntityGenerator
{
// ...
protected function generateEntityStubMethod(ClassMetadataInfo $metadata, $type, $fieldName, $typeHint = null, $defaultValue = null)
{
$methodName = $type . Inflector::classify($fieldName);
+ $variableName = $fieldName;
if (in_array($type, array("add", "remove"))) {
$methodName = Inflector::singularize($methodName);
+ $variableName = Inflector::singularize($variableName);
}
// ...
$replacements = array(
- '<description>' => ucfirst($type) . ' ' . $fieldName,
+ '<description>' => ucfirst($type) . ' ' . $variableName,
'<methodTypeHint>' => $methodTypeHint,
'<variableType>' => $variableType,
- '<variableName>' => Inflector::camelize($fieldName),
+ '<variableName>' => Inflector::camelize($variableName),
'<methodName>' => $methodName,
'<fieldName>' => $fieldName,
'<variableDefault>' => ($defaultValue !== null ) ? (' = '.$defaultValue) : '',
'<entity>' => $this->getClassName($metadata)
);
// ...
}
// ...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment