Browse files

Added defaultValue support for type ARRAY

  • Loading branch information...
1 parent 3fc74cc commit f15d36dd6e4bdc2a723e5af0fe2c1954e972387d @willdurand willdurand committed Jun 27, 2012
View
2 generator/lib/builder/om/PHP5ObjectBuilder.php
@@ -165,6 +165,8 @@ protected function getDefaultValueString(Column $col)
$defaultValue = var_export($val, true);
} elseif ($col->isPhpObjectType()) {
$defaultValue = 'new '.$col->getPhpType().'(' . var_export($val, true) . ')';
+ } elseif ($col->isPhpArrayType()) {
+ $defaultValue = var_export($val, true);
} else {
throw new EngineException("Cannot get default value string for " . $col->getFullyQualifiedName());
}
View
9 generator/lib/model/Column.php
@@ -935,6 +935,15 @@ public function isTemporalType()
}
/**
+ * Utility method to know whether column is an array column.
+ * @return boolean
+ */
+ public function isPhpArrayType()
+ {
+ return PropelTypes::isPhpArrayType($this->getType());
+ }
+
+ /**
* Utility method to know whether column is an ENUM column.
* @return boolean
*/
View
23 generator/lib/model/Domain.php
@@ -271,16 +271,21 @@ public function getPhpDefaultValue()
{
if ($this->defaultValue === null) {
return null;
- } else {
- if ($this->defaultValue->isExpression()) {
- throw new EngineException("Cannot get PHP version of default value for default value EXPRESSION.");
- }
- if ($this->propelType === PropelTypes::BOOLEAN || $this->propelType === PropelTypes::BOOLEAN_EMU) {
- return $this->booleanValue($this->defaultValue->getValue());
- } else {
- return $this->defaultValue->getValue();
- }
}
+
+ if ($this->defaultValue->isExpression()) {
+ throw new EngineException("Cannot get PHP version of default value for default value EXPRESSION.");
+ }
+
+ if ($this->propelType === PropelTypes::BOOLEAN || $this->propelType === PropelTypes::BOOLEAN_EMU) {
+ return $this->booleanValue($this->defaultValue->getValue());
+ }
+
+ if (PropelTypes::PHP_ARRAY === $this->propelType) {
+ return $this->getDefaultValueForArray($this->defaultValue->getValue());
+ }
+
+ return $this->defaultValue->getValue();
}
/**
View
13 generator/lib/model/PropelTypes.php
@@ -365,10 +365,21 @@ public static function isPhpPrimitiveNumericType($phpType)
* Convenience method to indicate whether a passed-in PHP type is an object.
*
* @param string $phpType The PHP type to check
- * @return boolean Whether the PHP type is a primitive (string, int, boolean, float)
+ * @return boolean
*/
public static function isPhpObjectType($phpType)
{
return (!self::isPhpPrimitiveType($phpType) && !in_array($phpType, array("resource", "array")));
}
+
+ /**
+ * Convenience method to indicate whether a passed-in PHP type is an array.
+ *
+ * @param string $phpType The PHP type to check
+ * @return boolean
+ */
+ public static function isPhpArrayType($phpType)
+ {
+ return strtoupper($phpType) === self::PHP_ARRAY;
+ }
}
View
21 generator/lib/model/XMLElement.php
@@ -91,6 +91,27 @@ protected function booleanValue($val)
}
}
+ protected function getDefaultValueForArray($stringValue)
+ {
+ $stringValue = trim($stringValue);
+
+ if (empty($stringValue)) {
+ return null;
+ }
+
+ $values = array();
+ foreach (explode(',', $stringValue) as $v) {
+ $values[] = trim($v);
+ }
+
+ $value = implode($values, ' | ');
+ if (empty($value) || ' | ' === $value) {
+ return null;
+ }
+
+ return sprintf('||%s||', $value);
+ }
+
/**
* Appends DOM elements to represent this object in XML.
* @param DOMNode $node
View
29 generator/lib/platform/DefaultPlatform.php
@@ -363,6 +363,14 @@ public function getColumnDefaultValueDDL(Column $col)
$default .= $this->getBooleanString($defaultValue->getValue());
} elseif ($col->getType() == PropelTypes::ENUM) {
$default .= array_search($defaultValue->getValue(), $col->getValueSet());
+ } elseif ($col->isPhpArrayType()) {
+ $value = $this->getPhpArrayString($defaultValue->getValue());
+
+ if (null === $value) {
+ $default = '';
+ } else {
+ $default .= $value;
+ }
} else {
$default .= $defaultValue->getValue();
}
@@ -1130,6 +1138,27 @@ public function getBooleanString($b)
return ($b ? '1' : '0');
}
+ public function getPhpArrayString($stringValue)
+ {
+ $stringValue = trim($stringValue);
+
+ if (empty($stringValue)) {
+ return null;
+ }
+
+ $values = array();
+ foreach (explode(',', $stringValue) as $v) {
+ $values[] = trim($v);
+ }
+
+ $value = implode($values, ' | ');
+ if (empty($value) || ' | ' === $value) {
+ return null;
+ }
+
+ return $this->quote(sprintf('||%s||', $value));
+ }
+
/**
* Gets the preferred timestamp formatter for setting date/time values.
* @return string
View
56 test/testsuite/generator/builder/om/GeneratedObjectArrayColumnTypeTest.php
@@ -28,11 +28,15 @@ public function setUp()
<column name="id" primaryKey="true" type="INTEGER" autoIncrement="true" />
<column name="tags" type="ARRAY" />
<column name="value_set" type="ARRAY" />
+ <column name="defaults" type="ARRAY" defaultValue="FOO" />
+ <column name="multiple_defaults" type="ARRAY" defaultValue="FOO, BAR,BAZ" />
</table>
</database>
EOF;
PropelQuickBuilder::buildSchema($schema);
}
+
+ ComplexColumnTypeEntity2Peer::doDeleteAll();
}
public function testActiveRecordMethods()
@@ -53,7 +57,57 @@ public function testActiveRecordMethods()
public function testGetterDefaultValue()
{
$e = new ComplexColumnTypeEntity2();
- $this->assertEquals(array(), $e->getTags(), 'array columns return an empty array by default');
+ $this->assertEquals(array(), $e->getValueSet(), 'array columns return an empty array by default');
+ }
+
+ public function testGetterDefaultValueWithData()
+ {
+ $e = new ComplexColumnTypeEntity2();
+ $this->assertEquals(array('FOO'), $e->getDefaults());
+ }
+
+ public function testGetterDefaultValueWithMultipleData()
+ {
+ $e = new ComplexColumnTypeEntity2();
+ $this->assertEquals(array('FOO', 'BAR', 'BAZ'), $e->getMultipleDefaults());
+ }
+
+ public function testAdderAddsNewValueToExistingData()
+ {
+ $e = new ComplexColumnTypeEntity2();
+ $this->assertEquals(array('FOO'), $e->getDefaults());
+ $e->addDefault('bar');
+ $this->assertEquals(array('FOO', 'bar'), $e->getDefaults());
+ }
+
+ public function testAdderAddsNewValueToMultipleExistingData()
+ {
+ $e = new ComplexColumnTypeEntity2();
+ $this->assertEquals(array('FOO', 'BAR', 'BAZ'), $e->getMultipleDefaults());
+ $e->addMultipleDefault('bar');
+ $this->assertEquals(array('FOO', 'BAR', 'BAZ', 'bar'), $e->getMultipleDefaults());
+ }
+
+ public function testDefaultValuesAreWellPersisted()
+ {
+ $e = new ComplexColumnTypeEntity2();
+ $e->save();
+
+ ComplexColumnTypeEntity2Peer::clearInstancePool();
+ $e = ComplexColumnTypeEntity2Query::create()->findOne();
+
+ $this->assertEquals(array('FOO'), $e->getDefaults());
+ }
+
+ public function testMultipleDefaultValuesAreWellPersisted()
+ {
+ $e = new ComplexColumnTypeEntity2();
+ $e->save();
+
+ ComplexColumnTypeEntity2Peer::clearInstancePool();
+ $e = ComplexColumnTypeEntity2Query::create()->findOne();
+
+ $this->assertEquals(array('FOO', 'BAR', 'BAZ'), $e->getMultipleDefaults());
}
public function testSetterArrayValue()
View
8 test/testsuite/generator/model/ColumnTest.php
@@ -200,4 +200,12 @@ public function testHasPlatform()
$this->assertTrue($column->hasPlatform());
}
+ public function testIsPhpArrayType()
+ {
+ $column = new Column();
+ $this->assertFalse($column->isPhpArrayType());
+
+ $column->setType(PropelTypes::PHP_ARRAY);
+ $this->assertTrue($column->isPhpArrayType());
+ }
}
View
56 test/testsuite/generator/model/XMLElementTest.php
@@ -0,0 +1,56 @@
+<?php
+
+/**
+ * This file is part of the Propel package.
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ *
+ * @license MIT License
+ */
+
+require_once dirname(__FILE__) . '/../../../../generator/lib/model/XMLElement.php';
+
+/**
+ * @author William Durand <william.durand1@gmail.com>
+ */
+class XMLElementTest extends PHPUnit_Framework_TestCase
+{
+ /**
+ * @dataProvider providerForGetDefaultValueForArray
+ */
+ public function testGetDefaultValueForArray($value, $expected)
+ {
+ $xmlElement = new TestableXmlElement();
+ $this->assertEquals($expected, $xmlElement->getDefaultValueForArray($value));
+ }
+
+ public static function providerForGetDefaultValueForArray()
+ {
+ return array(
+ array('', null),
+ array(null, null),
+ array('FOO', '||FOO||'),
+ array('FOO, BAR', '||FOO | BAR||'),
+ array('FOO , BAR', '||FOO | BAR||'),
+ array('FOO,BAR', '||FOO | BAR||'),
+ array(' ', null),
+ array(', ', null),
+ );
+ }
+}
+
+class TestableXmlElement extends XMLElement
+{
+ public function getDefaultValueForArray($value)
+ {
+ return parent::getDefaultValueForArray($value);
+ }
+
+ public function appendXml(DOMNode $node)
+ {
+ }
+
+ protected function setupObject()
+ {
+ }
+}

0 comments on commit f15d36d

Please sign in to comment.