Chain of responsibility and handlers dependent on each other I'm importing data from file to many tables in database. It may look like this:

First Name, Last Name, Meeting
John, Doe, 2020-04-24 08:00:00


Some fields like "First Name" and "Last Name" are being saved in one table and other fields like "Meeting" are being saved in different tables. There are a lot of more fields and a lot of more rows in my case.

I create handler for each table and put it in a handler chain to reduce amount of code. Each handler knows which fields it can handle. My base handler class looks like this:

abstract class TableHandler
{
    protected $nextHandler;

    abstract public function isFieldSupported(string $field): bool;
    abstract public function handle(string $fieldName, string $fieldValue);

    public function next(TableHandler $nextHandler): TableHandler
    {
        $this-&gt;nextHandler = $nextHandler;
    }

    public function handleField(string $fieldName, string $fieldValue)
    {
        $this-&gt;handle($fieldName, $fieldValue);

        if ($this-&gt;nextHandler()) {
            $this-&gt;nextHandler-&gt;handleField($fieldName, $fieldValue);
        }
    }

    public function reset(): void
    {
        $this-&gt;resetHandler();

        if ($this-&gt;nextHandler()) {
            $this-&gt;nextHandler-&gt;reset();
        }
    }

    private function nextHandler(): bool
    {
        return $this-&gt;nextHandler !== null;
    }
}


I use my handler chain this way:

$table1-&gt;next($table2)
    -&gt;next($table3);

foreach ($data as $row) {
    foreach ($headers as $index =&gt; $header) {
        $table1-&gt;handleField($header, $row[$index]);
    }

    $id = $table1-&gt;save();
    $table2-&gt;save($id);
    $table3-&gt;save($id);

    $table1-&gt;reset();
}


As you can see I was able to handle fields and reset all handlers by using only first handler. Chain does the rest of the job. The problem is that I don't know how can I improve saving data. When I insert record in first table I get its ID. I need to pass this ID to further handlers because rows that I insert in those tables are related to row from first table so ID is needed. Bacause of it I need to save data separately. I would like to avoid it. Is it possible?
I'm not to sure about your base handler. It has methods like handle() and handleField() that look very similar. One will probably do. I also noted that the save() method is completely missing from your code. Why? You use it, so it should be there. Or at least in an usage example.

As to your actual problem with saving, you can hand over the id's as an argument to the next save() in the clain. Something like:

public function save(array $insertIds = [])
{
    // ..... put needed insert ids into data here ....
    // then perform the insert and get the new insert id 
    $newInsertId = $this-&gt;insertRow($this-&gt;table,$this-&gt;data);
    // add that to the array of insert ids
    $insertIds[$this-&gt;table] = $newInsertId;
    // progress down the chain, if possible
    if ($this-&gt;nextHandler()) {
        $this-&gt;nextHandler-&gt;save($insertIds);
    }
}


Note that in the $insertIds array the keys are table names, and the values are the actual insert ids. This way all insert ids are available to save() methods down the chain.

It could be useful to return the $insertIds.

Note that the order of chaining is important. You have to think about that when you create the chain. Perhaps it would be a good idea to allow for multiple next handlers.