dynamically add new Entry to row #256
-
I do have one requirement where i need to add a new entry after a transformation. The pseudo-code would be something like this.
It seems like this is not a supported case at the moment. What i do instead is to create a new Rows-object instead. So something like this:
|
Beta Was this translation helpful? Give feedback.
Replies: 4 comments
-
Hey, not sure if I fully understand your use case, could you maybe provide example of what you are trying to achieve in following format: before:
after:
|
Beta Was this translation helpful? Give feedback.
-
This is exactly what i am trying to do. I want to add a new entry to the existing row. However if i call this method
|
Beta Was this translation helpful? Give feedback.
-
Oh I see, so here are a few examples of how you can achieve it. <?php
use Flow\ETL\DSL\Entry;
use Flow\ETL\DSL\From;
use Flow\ETL\DSL\To;
use Flow\ETL\DSL\Transform;
use Flow\ETL\Flow;
use Flow\ETL\Memory\ArrayMemory;
use Flow\ETL\Row;
use Flow\ETL\Rows;
use Flow\ETL\Transformer;
require __DIR__ . '/../vendor/autoload.php';
(new Flow())
->read(From::array(
[
['entry_a' => 'value', 'entry_b' => 'value'],
['entry_a' => 'value', 'entry_b' => 'value']
]
))
->rows(Transform::array_unpack('row'))
->drop('row')
->write(To::stdout())
->rows(Transform::add_integer('extra_value', \random_int(1, 5)))
->write(To::stdout())
->run();
(new Flow())
->read(From::array(
[
['entry_a' => 'value', 'entry_b' => 'value'],
['entry_a' => 'value', 'entry_b' => 'value']
]
))
->rows(Transform::array_unpack('row'))
->drop('row')
->write(To::stdout())
->map(static fn (Row $row) : Row => $row->add(Entry::integer('extra_value', \random_int(1, 5))))
->write(To::stdout())
->run();
(new Flow())
->read(From::array(
[
['entry_a' => 'value', 'entry_b' => 'value'],
['entry_a' => 'value', 'entry_b' => 'value']
]
))
->rows(Transform::array_unpack('row'))
->drop('row')
->write(To::stdout())
->transform(
new class implements Transformer {
public function transform(Rows $rows): Rows
{
return $rows->map(function (Row $row) : Row {
return $row->add(Entry::integer('extra_value', \random_int(1, 10)));
});
}
public function __unserialize(array $data): void
{
}
public function __serialize(): array
{
return [];
}
}
)
->write(To::stdout())
->run(); Let me know if that solves your problem, output of above script, below:
|
Beta Was this translation helpful? Give feedback.
-
This examples helped me a lot. However i think i will go with the last approach using the custom transformer as it better fits my scenario. |
Beta Was this translation helpful? Give feedback.
Oh I see, so here are a few examples of how you can achieve it.
You were almost good, but you need to remember that
Rows
is immutable, which means that while iterating using foreach even if you change something at a singleRow
it wont affectRows
.To adjust
Rows
you should useRows::map(static fn (Row $row) : Row => $row)
that will return a new instance ofRows
that would include your changes at each Row you made through the anonymous function.