Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

pre-defined key type for object builder. #216

Closed
wants to merge 1 commit into from

3 participants

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on May 11, 2012
  1. pre-defined key type for object builder.

    Denys Chmeliuk authored
This page is out of date. Refresh to see the latest.
View
34 src/Propel/Generator/Builder/Om/ObjectBuilder.php
@@ -52,6 +52,17 @@ public function getNamespace()
}
/**
+ * Returns default key type. if not presented in configuration default will be 'TYPE_PHPNAME'
+ * @return string
+ */
+ public function getDefaultKeyType()
+ {
+ $defaultKeyType = $this->getBuildProperty('defaultKeyType') ? $this->getBuildProperty('defaultKeyType') : 'phpName';
+
+ return "TYPE_".strtoupper($defaultKeyType);
+ }
+
+ /**
* Returns the name of the current class being built.
* @return string
*/
@@ -2126,6 +2137,7 @@ protected function addToArray(&$script)
$hasFks = count($fks) > 0 || count($referrers) > 0;
$objectClassName = $this->getUnqualifiedClassName();
$pkGetter = $this->getTable()->hasCompositePrimaryKey() ? 'serialize($this->getPrimaryKey())' : '$this->getPrimaryKey()';
+ $defaultKeyType = $this->getDefaultKeyType();
$script .= "
/**
* Exports the object as an array.
@@ -2135,7 +2147,7 @@ protected function addToArray(&$script)
*
* @param string \$keyType (optional) One of the class type constants BasePeer::TYPE_PHPNAME, BasePeer::TYPE_STUDLYPHPNAME,
* BasePeer::TYPE_COLNAME, BasePeer::TYPE_FIELDNAME, BasePeer::TYPE_NUM.
- * Defaults to BasePeer::TYPE_PHPNAME.
+ * Defaults to BasePeer::$defaultKeyType.
* @param boolean \$includeLazyLoadColumns (optional) Whether to include lazy loaded columns. Defaults to TRUE.
* @param array \$alreadyDumpedObjects List of objects to skip to avoid recursion";
if ($hasFks) {
@@ -2146,7 +2158,7 @@ protected function addToArray(&$script)
*
* @return array an associative array containing the field names (as keys) and field values
*/
- public function toArray(\$keyType = BasePeer::TYPE_PHPNAME, \$includeLazyLoadColumns = true, \$alreadyDumpedObjects = array()" . ($hasFks ? ", \$includeForeignObjects = false" : '') . ")
+ public function toArray(\$keyType = BasePeer::$defaultKeyType, \$includeLazyLoadColumns = true, \$alreadyDumpedObjects = array()" . ($hasFks ? ", \$includeForeignObjects = false" : '') . ")
{
if (isset(\$alreadyDumpedObjects['$objectClassName'][$pkGetter])) {
return '*RECURSION*';
@@ -2216,6 +2228,7 @@ protected function addGetByName(&$script)
**/
protected function addGetByNameComment(&$script)
{
+ $defaultKeyType = $this->getDefaultKeyType();
$script .= "
/**
* Retrieves a field from the object by name passed in as a string.
@@ -2223,7 +2236,8 @@ protected function addGetByNameComment(&$script)
* @param string \$name name
* @param string \$type The type of fieldname the \$name is of:
* one of the class type constants BasePeer::TYPE_PHPNAME, BasePeer::TYPE_STUDLYPHPNAME
- * BasePeer::TYPE_COLNAME, BasePeer::TYPE_FIELDNAME, BasePeer::TYPE_NUM
+ * BasePeer::TYPE_COLNAME, BasePeer::TYPE_FIELDNAME, BasePeer::TYPE_NUM.
+ * Defaults to BasePeer::$defaultKeyType.
* @return mixed Value of field.
*/";
}
@@ -2235,8 +2249,9 @@ protected function addGetByNameComment(&$script)
**/
protected function addGetByNameOpen(&$script)
{
+ $defaultKeyType = $this->getDefaultKeyType();
$script .= "
- public function getByName(\$name, \$type = BasePeer::TYPE_PHPNAME)
+ public function getByName(\$name, \$type = BasePeer::$defaultKeyType)
{";
}
@@ -2347,6 +2362,7 @@ protected function addGetByPositionClose(&$script)
protected function addSetByName(&$script)
{
+ $defaultKeyType = $this->getDefaultKeyType();
$script .= "
/**
* Sets a field from the object by name passed in as a string.
@@ -2355,10 +2371,11 @@ protected function addSetByName(&$script)
* @param mixed \$value field value
* @param string \$type The type of fieldname the \$name is of:
* one of the class type constants BasePeer::TYPE_PHPNAME, BasePeer::TYPE_STUDLYPHPNAME
- * BasePeer::TYPE_COLNAME, BasePeer::TYPE_FIELDNAME, BasePeer::TYPE_NUM
+ * BasePeer::TYPE_COLNAME, BasePeer::TYPE_FIELDNAME, BasePeer::TYPE_NUM.
+ * Defaults to BasePeer::$defaultKeyType.
* @return void
*/
- public function setByName(\$name, \$value, \$type = BasePeer::TYPE_PHPNAME)
+ public function setByName(\$name, \$value, \$type = BasePeer::$defaultKeyType)
{
\$pos = ".$this->getPeerClassName()."::translateFieldName(\$name, \$type, BasePeer::TYPE_NUM);
@@ -2416,6 +2433,7 @@ public function setByPosition(\$pos, \$value)
protected function addFromArray(&$script)
{
+ $defaultKeyType = $this->getDefaultKeyType();
$table = $this->getTable();
$script .= "
/**
@@ -2429,13 +2447,13 @@ protected function addFromArray(&$script)
* You can specify the key type of the array by additionally passing one
* of the class type constants BasePeer::TYPE_PHPNAME, BasePeer::TYPE_STUDLYPHPNAME,
* BasePeer::TYPE_COLNAME, BasePeer::TYPE_FIELDNAME, BasePeer::TYPE_NUM.
- * The default key type is the column's phpname (e.g. 'AuthorId')
+ * The default key type is the column's BasePeer::$defaultKeyType.
*
* @param array \$arr An array to populate the object from.
* @param string \$keyType The type of keys the array uses.
* @return void
*/
- public function fromArray(\$arr, \$keyType = BasePeer::TYPE_PHPNAME)
+ public function fromArray(\$arr, \$keyType = BasePeer::$defaultKeyType)
{
\$keys = ".$this->getPeerClassName()."::getFieldNames(\$keyType);
";
View
28 tests/Propel/Tests/Generator/Builder/Om/GeneratedObjectTest.php
@@ -49,8 +49,12 @@
use Propel\Runtime\Query\Criteria;
use Propel\Runtime\Util\BasePeer;
+use Propel\Generator\Util\QuickBuilder;
+
use \DateTime;
+use MyNameSpace\TestKeyTypeTable;
+
/**
* Tests the generated Object classes.
*
@@ -749,6 +753,30 @@ public function testToArrayKeyType()
$this->assertEquals('Don Juan', $arr1[BookPeer::TITLE], 'toArray() returns an associative array representation of the object');
}
+ public function testToArrayKeyTypePreDefined()
+ {
+ $schema = <<<EOF
+<database name="test" namespace="MyNameSpace">
+ <table name="test_key_type_table">
+ <column name="id_key_type" required="true" primaryKey="true" autoIncrement="true" type="INTEGER" />
+ <column name="name_key_type" type="VARCHAR" />
+ </table>
+</database>
+EOF;
+
+ $builder = new QuickBuilder();
+ $builder->setSchema($schema);
+ $builder->getConfig()->setBuildProperty('defaultKeyType', 'studlyPhpName');
+ $builder->buildClasses();
+
+ $expectedKeys = array(
+ 'idKeyType',
+ 'nameKeyType',
+ );
+ $object = new TestKeyTypeTable();
+ $this->assertEquals($expectedKeys, array_keys($object->toArray()), 'toArray() returns an associative array with pre-defined key type in properties.');
+ }
+
/**
* Test that setting the auto-increment primary key will result in exception.
*/
View
4 tests/Propel/Tests/Generator/Builder/Om/ObjectBuilderTest.php
@@ -64,6 +64,10 @@ public function testGetDefaultValueString($column, $value)
$this->assertEquals($value, $this->builder->getDefaultValueString($column));
}
+ public function testGetDefaultKeyType()
+ {
+ $this->assertEquals('TYPE_PHPNAME', $this->builder->getDefaultKeyType());
+ }
}
View
2  tools/generator/default.properties
@@ -101,6 +101,8 @@ propel.defaultTimeStampFormat =
propel.defaultTimeFormat =
propel.defaultDateFormat =
+propel.defaultKeyType = phpName
+
propel.namespace.om = Base
propel.namespace.map = Map
propel.namespace.autoPackage = true
Something went wrong with that request. Please try again.