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

add output groups behavior #9

Merged
merged 1 commit into from
Apr 25, 2024
Merged

Conversation

mringler
Copy link
Owner

@mringler mringler commented Apr 22, 2024

Allows declaring output groups on columns and foreign key relations in schema.xml. Passing a group name to the toOutputGroup() method on the model or ObjectCollection returns an array holding all values defined by the group name.

Usage

Declare groups in schema.xml:

<table name="le_table">
  <column name="col1" outputGroup="foo,bar" />
  <column name="col2" />
  <foreign-key phpName="fk" outputGroup="foo">
    ...

Serialize objects by output group:

leTableObject->toOutputGroup('foo'); // will return ['Col1' => ..., 'Fk' => ...]
leTableObject->toOutputGroup('bar'); // will return ['Col1' => ...]

Instead of a group name, an array with model class names can also be used. In this form, multiple groups can be given in an array:

leTableObject->toOutputGroup([
  LeTable::class => ['foo', 'bar'],
  'default' => 'bar'
]);

Default output groups per table

Default output groups can be defined on the table, columns and relations can opt-out with ignoreGroup or refIgnoreGroup:

<table name="le_table" outputGroup="foo,bar">
  <column name="col1" ignoreGroup="foo" />
  <column name="col2" />
  <foreign-key phpName="fk" ignoreGroup="foo" refIgnoreGroup="bar">
    ...

Avoids recursion

Related model instances are linked in both directions, leading to recursive output when using toArray(). Output groups skip relations that were already handled:

toArray() toOutputGroup()
[
  'event' => [
    'event_type' => [
      'events' => [[*RECURSION*]]
    ]
  ]
]
[
  'event' => [
    'event_type' => [
    ]
  ]
]

Implementation

Changes are implemented as a behavior, so this is an opt-in, and the code does not interfere with Propel code.

To enable output groups, the behavior has to be declared in schema.xml:

<database>
    <behavior name="output_group"/>

The behavior adds functionality to table map, model and query classes:

TableMap classes:

  • Add output groups from schema into TableMap classes and a method to access them.
  • Override TableMap::getCollectionClassName(), so queries use a subclass of ObjectCollection with a toOutputGroup() method. If necessary, the class can be set manually through the behavior's object_collection_class parameter.

Model classes:

  • Add toOutputGroup() method to generated model classes.
  • Add imlements interface declaration.

Query classes:

  • Add @method declarations with updated return type for the findX() methods to header doc.

I find this much easier than using a serializer as I don't have to re-iterate all columns in another file.

@mringler mringler force-pushed the feature/output_group_behavior branch 2 times, most recently from 1d95f06 to 0d622c5 Compare April 22, 2024 10:18
@mringler mringler mentioned this pull request Apr 22, 2024
@mringler mringler force-pushed the feature/output_group_behavior branch from 4504016 to 98409a4 Compare April 25, 2024 10:06
@mringler mringler changed the base branch from master to main April 25, 2024 10:16
@mringler mringler merged commit cedf1e9 into main Apr 25, 2024
33 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant