From 2653fbd60ea38b4970df8810f6176eb83785128a Mon Sep 17 00:00:00 2001 From: MagicLegend Date: Thu, 5 Nov 2020 17:33:04 +0200 Subject: [PATCH] Added support for keytype in the magic import/export methods --- .../templates/baseObjectMethodMagicCall.php | 6 ++-- .../Om/templates/baseObjectMethods.php | 5 ++-- src/Propel/Runtime/Collection/Collection.php | 8 ++++-- .../Runtime/Collection/OnDemandCollection.php | 2 +- .../Tests/Runtime/ActiveRecordConvertTest.php | 28 +++++++++++++++---- 5 files changed, 35 insertions(+), 14 deletions(-) diff --git a/src/Propel/Generator/Builder/Om/templates/baseObjectMethodMagicCall.php b/src/Propel/Generator/Builder/Om/templates/baseObjectMethodMagicCall.php index 608e272c30..f0e291328e 100644 --- a/src/Propel/Generator/Builder/Om/templates/baseObjectMethodMagicCall.php +++ b/src/Propel/Generator/Builder/Om/templates/baseObjectMethodMagicCall.php @@ -46,15 +46,17 @@ public function __($na if (0 === strpos($name, 'from')) { $format = substr($name, 4); + $keyType = isset($params[0]) ? $params[0] : TableMap::TYPE_PHPNAME; - return $this->importFrom($format, reset($params)); + return $this->importFrom($format, reset($params), $keyType); } if (0 === strpos($name, 'to')) { $format = substr($name, 2); $includeLazyLoadColumns = isset($params[0]) ? $params[0] : true; + $keyType = isset($params[1]) ? $params[1] : TableMap::TYPE_PHPNAME; - return $this->exportTo($format, $includeLazyLoadColumns); + return $this->exportTo($format, $includeLazyLoadColumns, $keyType); } throw new BadMethodCallException(sprintf('Call to undefined method: %s.', $name)); diff --git a/src/Propel/Generator/Builder/Om/templates/baseObjectMethods.php b/src/Propel/Generator/Builder/Om/templates/baseObjectMethods.php index 065155f070..1956cf2bae 100644 --- a/src/Propel/Generator/Builder/Om/templates/baseObjectMethods.php +++ b/src/Propel/Generator/Builder/Om/templates/baseObjectMethods.php @@ -187,15 +187,16 @@ protected function log($msg, $priority = Propel::LOG_INFO) * * @param mixed $parser A AbstractParser instance, or a format name ('XML', 'YAML', 'JSON', 'CSV') * @param boolean $includeLazyLoadColumns (optional) Whether to include lazy load(ed) columns. Defaults to TRUE. + * @param string $keyType (optional) One of the class type constants TableMap::TYPE_PHPNAME, TableMap::TYPE_CAMELNAME, TableMap::TYPE_COLNAME, TableMap::TYPE_FIELDNAME, TableMap::TYPE_NUM. Defaults to TableMap::TYPE_PHPNAME. * @return string The exported data */ - public function exportTo($parser, $includeLazyLoadColumns = true) + public function exportTo($parser, $includeLazyLoadColumns = true, $keyType = TableMap::TYPE_PHPNAME) { if (!$parser instanceof AbstractParser) { $parser = AbstractParser::getParser($parser); } - return $parser->fromArray($this->toArray(TableMap::TYPE_PHPNAME, $includeLazyLoadColumns, array(), true)); + return $parser->fromArray($this->toArray($keyType, $includeLazyLoadColumns, array(), true)); } /** diff --git a/src/Propel/Runtime/Collection/Collection.php b/src/Propel/Runtime/Collection/Collection.php index c5121d133d..b82a54522e 100644 --- a/src/Propel/Runtime/Collection/Collection.php +++ b/src/Propel/Runtime/Collection/Collection.php @@ -556,16 +556,17 @@ public function importFrom($parser, $data) * @param bool $includeLazyLoadColumns (optional) Whether to include lazy load(ed) columns. Defaults to TRUE. * Not supported by ArrayCollection, as ArrayFormatter has * already included lazy-load columns in the array used here. + * @param string $keyType (optional) One of the class type constants TableMap::TYPE_PHPNAME, TableMap::TYPE_CAMELNAME, TableMap::TYPE_COLNAME, TableMap::TYPE_FIELDNAME, TableMap::TYPE_NUM. Defaults to TableMap::TYPE_PHPNAME. * * @return string The exported data */ - public function exportTo($parser, $usePrefix = true, $includeLazyLoadColumns = true) + public function exportTo($parser, $usePrefix = true, $includeLazyLoadColumns = true, $keyType = TableMap::TYPE_PHPNAME) { if (!$parser instanceof AbstractParser) { $parser = AbstractParser::getParser($parser); } - $array = $this->toArray(null, $usePrefix, TableMap::TYPE_PHPNAME, $includeLazyLoadColumns); + $array = $this->toArray(null, $usePrefix, $keyType, $includeLazyLoadColumns); return $parser->listFromArray($array, $this->getPluralModelName()); } @@ -594,8 +595,9 @@ public function __call($name, $params) $format = substr($name, 2); $usePrefix = isset($params[0]) ? $params[0] : false; $includeLazyLoadColumns = isset($params[1]) ? $params[1] : true; + $keyType = isset($params[2]) ? $params[2] : TableMap::TYPE_PHPNAME; - return $this->exportTo($format, $usePrefix, $includeLazyLoadColumns); + return $this->exportTo($format, $usePrefix, $includeLazyLoadColumns, $keyType); } throw new BadMethodCallException('Call to undefined method: ' . $name); diff --git a/src/Propel/Runtime/Collection/OnDemandCollection.php b/src/Propel/Runtime/Collection/OnDemandCollection.php index 2d6c41bd22..523404bc3b 100644 --- a/src/Propel/Runtime/Collection/OnDemandCollection.php +++ b/src/Propel/Runtime/Collection/OnDemandCollection.php @@ -257,7 +257,7 @@ public function getArrayCopy() * * @throws \Propel\Runtime\Exception\PropelException */ - public function exportTo($parser, $usePrefix = true, $includeLazyLoadColumns = true) + public function exportTo($parser, $usePrefix = true, $includeLazyLoadColumns = true, $keyType = Propel\Runtime\Map\TableMap::TYPE_PHPNAME) { throw new PropelException('A OnDemandCollection cannot be exported.'); } diff --git a/tests/Propel/Tests/Runtime/ActiveRecordConvertTest.php b/tests/Propel/Tests/Runtime/ActiveRecordConvertTest.php index 151761711e..385e00c29f 100644 --- a/tests/Propel/Tests/Runtime/ActiveRecordConvertTest.php +++ b/tests/Propel/Tests/Runtime/ActiveRecordConvertTest.php @@ -8,6 +8,7 @@ namespace Propel\Tests\Runtime\ActiveRecord; +use Propel\Runtime\Map\TableMap; use Propel\Tests\Bookstore\Author; use Propel\Tests\Bookstore\Book; use Propel\Tests\Bookstore\Publisher; @@ -174,11 +175,26 @@ public function testFromYAML($expected) public function toJsonDataProvider() { - $expected = <<assertEquals($expected, $this->book->toJSON()); + $this->assertEquals($expected, $this->book->toJSON(true, $type)); } /** @@ -196,10 +212,10 @@ public function testToJSON($expected) * * @return void */ - public function testfromJSON($expected) + public function testfromJSON($expected, $type) { $book = new Book(); - $book->fromJSON($expected); + $book->fromJSON($expected, $type); // FIXME: fromArray() doesn't take related objects into account $book->resetModified(); $author = $this->book->getAuthor();