Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Issue #257

  • Loading branch information...
commit 8a3a5808ba1c63579137f07d5029736a9949d497 1 parent 4834bb6
@cristianoc72 cristianoc72 authored
View
2  .gitignore
@@ -31,3 +31,5 @@ tests/Fixtures/reverse/mysql/runtime-conf.xml
tests/Fixtures/reverse/pgsql/build/
tests/Fixtures/reverse/pgsql/runtime-conf.xml
+
+.idea
View
2  src/Propel/Generator/Builder/Om/ObjectBuilder.php
@@ -184,6 +184,8 @@ protected function getDefaultValueString(Column $column)
$defaultValue = var_export($val, true);
} elseif ($column->isPhpObjectType()) {
$defaultValue = 'new '.$column->getPhpType().'(' . var_export($val, true) . ')';
+ } elseif ($column->isPhpArrayType()) {
+ $defaultValue = var_export($val, true);
} else {
throw new EngineException("Cannot get default value string for " . $column->getFullyQualifiedName());
}
View
9 src/Propel/Generator/Model/Column.php
@@ -1047,6 +1047,15 @@ public function isTemporalType()
}
/**
+ * Utility method to know whether column is an array column.
+ * @return boolean
+ */
+ public function isPhpArrayType()
+ {
+ return PropelTypes::isPhpArrayType($this->getType());
+ }
+
+ /**
* Returns whether or not this column is an ENUM column.
*
* @return boolean
View
4 src/Propel/Generator/Model/Domain.php
@@ -281,6 +281,10 @@ public function getPhpDefaultValue()
return $this->booleanValue($this->defaultValue->getValue());
}
+ if (PropelTypes::PHP_ARRAY === $this->mappingType) {
+ return $this->getDefaultValueForArray($this->defaultValue->getValue());
+ }
+
return $this->defaultValue->getValue();
}
View
21 src/Propel/Generator/Model/MappingModel.php
@@ -101,6 +101,27 @@ protected function booleanValue($value)
return in_array(strtolower($value), array('true', 't', 'y', 'yes'), true);
}
+ 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.
*
View
11 src/Propel/Generator/Model/PropelTypes.php
@@ -364,4 +364,15 @@ 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
27 src/Propel/Generator/Platform/DefaultPlatform.php
@@ -381,6 +381,13 @@ 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();
}
@@ -1163,6 +1170,26 @@ public function getBooleanString($b)
return '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
54 tests/Propel/Tests/Generator/Builder/Om/GeneratedObjectArrayColumnTypeTest.php
@@ -35,11 +35,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;
QuickBuilder::buildSchema($schema);
}
+
+ ComplexColumnTypeEntity2Peer::doDeleteAll();
}
public function testActiveRecordMethods()
@@ -63,6 +67,56 @@ public function testGetterDefaultValue()
$this->assertEquals(array(), $e->getTags(), '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()
{
$e = new ComplexColumnTypeEntity2();
View
10 tests/Propel/Tests/Generator/Model/ColumnTest.php
@@ -10,6 +10,7 @@
namespace Propel\Tests\Generator\Model;
use Propel\Generator\Model\Column;
+use Propel\Generator\Model\PropelTypes;
/**
* Tests for package handling.
@@ -844,6 +845,15 @@ public function testHasPlatform()
$this->assertInstanceOf('Propel\Generator\Platform\PlatformInterface', $column->getPlatform());
}
+ public function testIsPhpArrayType()
+ {
+ $column = new Column();
+ $this->assertFalse($column->isPhpArrayType());
+
+ $column->setType(PropelTypes::PHP_ARRAY);
+ $this->assertTrue($column->isPhpArrayType());
+ }
+
public function testSetSize()
{
$domain = $this->getDomainMock();
View
58 tests/Propel/Tests/Generator/Model/MappingModelTest.php
@@ -0,0 +1,58 @@
+<?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
+ */
+
+namespace Propel\Tests\Generator\Model;
+
+use Propel\Generator\Model\MappingModel;
+
+/**
+ * @author William Durand <william.durand1@gmail.com>
+ */
+class MappingModelTest extends \PHPUnit_Framework_TestCase
+{
+ /**
+ * @dataProvider providerForGetDefaultValueForArray
+ */
+ public function testGetDefaultValueForArray($value, $expected)
+ {
+ $mappingModel = new TestableMappingModel();
+ $this->assertEquals($expected, $mappingModel->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 TestableMappingModel extends MappingModel
+{
+ public function getDefaultValueForArray($value)
+ {
+ return parent::getDefaultValueForArray($value);
+ }
+
+ public function appendXml(DOMNode $node)
+ {
+ }
+
+ protected function setupObject()
+ {
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.