Skip to content

MapperEvents->afterDelete() cannot except null but Table->deleteRowPerform() can out put null. #116

Description

@Rockburner

In the Mapper->delete() method the $pdoStatement variable is populated from the table->deleteRowPerform() method, which "can" return a PDOStatment, however, the deleteRowPerform() method can "also" output null, which is not an acceptable parameter type in the mapperevents->afterDelete() method.

I believe this might happen if the row in question had already been deleted from the db itself (my db has foreign keys which might be causing this situation), but in that case, if the delete of the row has already happened, then it may be preferable for the code to just "carry on", instead of throwing an exception.

public function delete(Record $record) : void
    {
        $row = $record->getRow();
        $this->mapperEvents->beforeDelete($this, $record);
        $this->relationships->fixNativeRecord($record);
        $delete = $this->table->deleteRowPrepare($row);
        $this->mapperEvents->modifyDelete($this, $record, $delete);
        $pdoStatement = $this->table->deleteRowPerform($row, $delete);
        $this->relationships->fixForeignRecord($record);
        $this->mapperEvents->afterDelete(
            $this,
            $record,
            $delete,
            $pdoStatement
        );
    }

Table->deleteRowPerform() method

    public function deleteRowPerform(Row $row, Delete $delete) : ?PDOStatement
    {
        if ($row->getStatus() === $row::DELETED) {
            return null;
        }

        if (empty(static::PRIMARY_KEY)) {
            throw Exception::cannotPerformWithoutPrimaryKey('delete row', static::NAME);
        }

        $pdoStatement = $delete->perform();

        $rowCount = $pdoStatement->rowCount();
        if ($rowCount != 1) {
            throw Exception::unexpectedRowCountAffected($rowCount);
        }

        $this->tableEvents->afterDeleteRow($this, $row, $delete, $pdoStatement);
        $row->init($row::DELETED);
        return $pdoStatement;
    }

Mapperevents->afterDelete() method.

    public function afterDelete(
        Mapper $mapper,
        Record $record,
        Delete $delete,
        PDOStatement $pdoStatement
    ) : void
    {
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions