Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/output group #8

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions src/Propel/Generator/Builder/Om/ObjectBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,7 @@ protected function addClassBody(string &$script): void
$this->addGetByName($script);
$this->addGetByPosition($script);
$this->addToArray($script);
$this->addToOutputGroup($script);
}

if ($this->isAddGenericMutators()) {
Expand Down Expand Up @@ -3105,6 +3106,89 @@ public function toArray(string \$keyType = TableMap::$defaultKeyType, bool \$inc

// addToArray()


/**
* @param string $script
*
* @return void
*/
protected function addToOutputGroup(string &$script): void
{
$script .= $this->renderTemplate('baseObjectToOutputGroup', [
'tableMapClassName' => $this->getTableMapClassName(),
'objectClassName' => $this->getUnqualifiedClassName(),
'defaultKeyType' => $this->getDefaultKeyType(),
'temporalColumnIndexesByFormatter' => $this->getTemporalColumnIndexesByFormatter(),
'relationFormatterData' => $this->buildRelationFormatterData(),
]);
}

/**
* Build a map with date format strings (i.e. 'Y-m-d') as keys and the
* indexes of associated column indexes as values.
*
* @return array<array<int>>
*/
protected function getTemporalColumnIndexesByFormatter(): array
{
$temporalColumnIndexesByFormatter = [];
foreach ($this->getTable()->getColumns() as $num => $col) {
if (!$col->isTemporalType()) {
continue;
}
$formatter = $this->getTemporalFormatter($col);
$temporalColumnIndexesByFormatter[$formatter][] = $num;
}

return $temporalColumnIndexesByFormatter;
}

/**
* Get relation data as used in output group template.
*
* @return array<array{
* 'localVariableName': string,
* 'relationName': string,
* 'targetKeyLookupStatement': string,
* 'isCollection': bool,
* 'relationId': string
* }>
*/
protected function buildRelationFormatterData(): array
{
$result = [];

$fks = $this->getTable()->getForeignKeys();
foreach ($fks as $fk) {
$lookup = $this->addToArrayKeyLookUp($fk->getPhpName(), $fk->getForeignTable(), false);
$result[] = [
'localVariableName' => $this->getFKVarName($fk),
'relationName' => $this->getFKPhpNameAffix($fk),
'targetKeyLookupStatement' => $lookup,
'isCollection' => false,
'relationId' => $fk->getName(),
];
}

$refs = $this->getTable()->getReferrers();
foreach ($refs as $ref) {
$isLocal = $ref->isLocalPrimaryKey();
$localVariableName = ($isLocal) ? $this->getPKRefFKVarName($ref) : $this->getRefFKCollVarName($ref);
$lookup = $this->addToArrayKeyLookUp($ref->getRefPhpName(), $ref->getTable(), !$isLocal);
/** @var string $relationName */
$relationName = $this->getRefFKPhpNameAffix($ref);
$result[] = [
'localVariableName' => $localVariableName,
'relationName' => $relationName,
'targetKeyLookupStatement' => $lookup,
'isCollection' => !$isLocal,
'relationId' => $ref->getName(),
];
}

return $result;
}

/**
* Adds the switch-statement for looking up the array-key name for toArray
*
Expand Down
59 changes: 59 additions & 0 deletions src/Propel/Generator/Builder/Om/TableMapBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@ protected function addClassBody(string &$script): void
$this->addDoDeleteAll($script);

$this->addDoInsert($script);

$script .= $this->addColumnOutputGroups();
}

/**
Expand Down Expand Up @@ -1596,4 +1598,61 @@ public static function doInsert(\$criteria, ?ConnectionInterface \$con = null)
}
";
}

/**
* Adds column output groups data.
*
* @return string
*/
protected function addColumnOutputGroups(): string
{
$outputGroups = [];
$this->collectColumnIndexesByOutputGroup($outputGroups);
$this->collectForeignKeysByOutputGroup($outputGroups);
ksort($outputGroups);

return $this->renderTemplate('tableMapOutputGroups', [
'columnIndexesByOutputGroup' => $outputGroups,
]);
}

/**
* @param array<array{'column_index'?: array<int>, 'relation'?: array<int>}> $outputGroups
*
* @return void
*/
protected function collectColumnIndexesByOutputGroup(array &$outputGroups)
{
foreach ($this->getTable()->getColumns() as $columnIndex => $column) {
$groupNames = $column->getOutputGroupNames();
foreach ($groupNames as $groupName) {
$outputGroups[$groupName]['column_index'][] = $columnIndex;
}
}
}

/**
* @param array<array{'column_index'?: array<int>, 'relation'?: array<int>}> $outputGroups
*
* @return void
*/
protected function collectForeignKeysByOutputGroup(array &$outputGroups)
{
$table = $this->getTable();

foreach ($table->getForeignKeys() as $fk) {
$groupNames = $fk->getLocalOutputGroupNames();
$fkName = $this->getFKPhpNameAffix($fk);
foreach ($groupNames as $groupName) {
$outputGroups[$groupName]['relation'][] = $fkName;
}
}
foreach ($table->getReferrers() as $ref) {
$groupNames = $ref->getRefOutputGroupNames();
$refName = $this->getRefFKPhpNameAffix($ref);
foreach ($groupNames as $groupName) {
$outputGroups[$groupName]['relation'][] = $refName;
}
}
}
}
25 changes: 25 additions & 0 deletions src/Propel/Generator/Model/Column.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,11 @@ class Column extends MappingModel
*/
protected $valueSet = [];

/**
* @var array<string>
*/
protected $outputGroupNames = [];

/**
* Creates a new column and set the name.
*
Expand Down Expand Up @@ -337,6 +342,8 @@ protected function setupObject(): void
$this->isAutoIncrement = $this->booleanValue($this->getAttribute('autoIncrement'));
$this->isLazyLoad = $this->booleanValue($this->getAttribute('lazyLoad'));

$this->outputGroupNames = $this->getDefaultValueForSet($this->getAttribute('outputGroup', '')) ?? [];

// Add type, size information to associated Domain object
$domain->replaceSqlType($this->getAttribute('sqlType'));

Expand Down Expand Up @@ -1686,6 +1693,24 @@ public function hasPlatform(): bool
return $this->parentTable->getPlatform() ? true : false;
}

/**
* @param array<string> $outputGroupNames
*
* @return void
*/
public function setOutputGroupNames(array $outputGroupNames)
{
$this->outputGroupNames = $outputGroupNames;
}

/**
* @return array<string>
*/
public function getOutputGroupNames(): array
{
return $this->outputGroupNames;
}

/**
* Clones the current object.
*
Expand Down
49 changes: 49 additions & 0 deletions src/Propel/Generator/Model/ForeignKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,16 @@ class ForeignKey extends MappingModel
*/
private $autoNaming = false;

/**
* @var array<string>
*/
protected $localOutputGroupNames = [];

/**
* @var array<string>
*/
protected $refOutputGroupNames = [];

/**
* Constructs a new ForeignKey object.
*
Expand Down Expand Up @@ -161,6 +171,9 @@ protected function setupObject(): void
$this->onUpdate = $this->normalizeFKey($this->getAttribute('onUpdate'));
$this->onDelete = $this->normalizeFKey($this->getAttribute('onDelete'));
$this->skipSql = $this->booleanValue($this->getAttribute('skipSql'));

$this->localOutputGroupNames = $this->getDefaultValueForSet($this->getAttribute('outputGroup', '')) ?? [];
$this->refOutputGroupNames = $this->getDefaultValueForSet($this->getAttribute('refOutputGroup', '')) ?? [];
}

/**
Expand Down Expand Up @@ -1128,4 +1141,40 @@ public function isAtLeastOneLocalPrimaryKey(): bool

return count($cols) !== 0;
}

/**
* @param array<string> $outputGroupNames
*
* @return void
*/
public function setLocalOutputGroupNames(array $outputGroupNames)
{
$this->localOutputGroupNames = $outputGroupNames;
}

/**
* @return array<string>
*/
public function getLocalOutputGroupNames(): array
{
return $this->localOutputGroupNames;
}

/**
* @param array<string> $outputGroupNames
*
* @return void
*/
public function setRefOutputGroupNames(array $outputGroupNames)
{
$this->refOutputGroupNames = $outputGroupNames;
}

/**
* @return array<string>
*/
public function getRefOutputGroupNames(): array
{
return $this->refOutputGroupNames;
}
}
1 change: 1 addition & 0 deletions src/Propel/Runtime/ActiveRecord/ActiveRecordInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* @author jaugustin
*
* @method array toArray(string $keyType = \Propel\Runtime\Map\TableMap::TYPE_FIELDNAME, bool $includeLazyLoadColumns = true, array $alreadyDumpedObjects = [], bool $includeForeignObjects = false): array
* @method array toOutputGroup($outputGroup, string $keyType = \Propel\Runtime\Map\TableMap::TYPE_FIELDNAME, array $alreadyDumpedObjects = []): array
*/
interface ActiveRecordInterface
{
Expand Down
34 changes: 34 additions & 0 deletions src/Propel/Runtime/Collection/ObjectCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,40 @@ public function toArray(
return $ret;
}

/**
* Turn collection objects into arrays with values specified by output group.
*
* @param array<string, string>|string $outputGroup Name of the output group used for all tables or
* an array mapping model classes to output group name.
* If a model class does not have a definition for the
* given output group, the whole data is returned.
* @param string|null $keyColumn (optional) Column name to use as index.
* @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.
* @param array $alreadyDumpedObjects Internally used on recursion, typically not set by user (List of
* objects to skip to avoid recursion).
*
* @return array
*/
public function toOutputGroup(
$outputGroup,
?string $keyColumn = null,
string $keyType = TableMap::TYPE_PHPNAME,
array $alreadyDumpedObjects = []
): array {
$ret = [];
$keyGetterMethod = 'get' . $keyColumn;

/** @var \Propel\Runtime\ActiveRecord\ActiveRecordInterface $obj */
foreach ($this->data as $key => $obj) {
$key = (!$keyColumn) ? $key : $obj->$keyGetterMethod();
$ret[$key] = $obj->toOutputGroup($outputGroup, $keyType, $alreadyDumpedObjects);
}

return $ret;
}

/**
* Get an array representation of the collection
*
Expand Down
Loading
Loading