Skip to content

Commit

Permalink
Added support for keytype in the magic import/export methods
Browse files Browse the repository at this point in the history
  • Loading branch information
MagicLegend authored and mringler committed Apr 6, 2021
1 parent 094b03d commit 2653fbd
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,17 @@ public function __<?php echo ($behaviorCallScript) ? 'parentCall' : 'call'?>($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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

/**
Expand Down
8 changes: 5 additions & 3 deletions src/Propel/Runtime/Collection/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/Propel/Runtime/Collection/OnDemandCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
}
Expand Down
28 changes: 22 additions & 6 deletions tests/Propel/Tests/Runtime/ActiveRecordConvertTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -174,32 +175,47 @@ public function testFromYAML($expected)

public function toJsonDataProvider()
{
$expected = <<<EOF
$phpName = <<<EOF
{"Id":9012,"Title":"Don Juan","ISBN":"0140422161","Price":12.99,"PublisherId":1234,"AuthorId":5678,"Publisher":{"Id":1234,"Name":"Penguin","Books":["*RECURSION*"]},"Author":{"Id":5678,"FirstName":"George","LastName":"Byron","Email":null,"Age":null,"Books":["*RECURSION*"]}}
EOF;
$camelName = <<<EOF
{"id":9012,"title":"Don Juan","isbn":"0140422161","price":12.99,"publisherId":1234,"authorId":5678,"publisher":{"id":1234,"name":"Penguin","books":["*RECURSION*"]},"author":{"id":5678,"firstName":"George","lastName":"Byron","email":null,"age":null,"books":["*RECURSION*"]}}
EOF;

return [[$expected]];
$colName = <<<EOF
{"book.id":9012,"book.title":"Don Juan","book.isbn":"0140422161","book.price":12.99,"book.publisher_id":1234,"book.author_id":5678,"Publisher":{"publisher.id":1234,"publisher.name":"Penguin","Books":["*RECURSION*"]},"Author":{"author.id":5678,"author.first_name":"George","author.last_name":"Byron","author.email":null,"author.age":null,"Books":["*RECURSION*"]}}
EOF;

$fieldName = <<<EOF
{"id":9012,"title":"Don Juan","isbn":"0140422161","price":12.99,"publisher_id":1234,"author_id":5678,"publisher":{"id":1234,"name":"Penguin","books":["*RECURSION*"]},"author":{"id":5678,"first_name":"George","last_name":"Byron","email":null,"age":null,"books":["*RECURSION*"]}}
EOF;


return [[$phpName, TableMap::TYPE_PHPNAME],
[$camelName, TableMap::TYPE_CAMELNAME],
[$colName, TableMap::TYPE_COLNAME],
[$fieldName, TableMap::TYPE_FIELDNAME]];
}

/**
* @dataProvider toJsonDataProvider
*
* @return void
*/
public function testToJSON($expected)
public function testToJSON($expected, $type)
{
$this->assertEquals($expected, $this->book->toJSON());
$this->assertEquals($expected, $this->book->toJSON(true, $type));
}

/**
* @dataProvider toJsonDataProvider
*
* @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();
Expand Down

0 comments on commit 2653fbd

Please sign in to comment.