Skip to content
This repository was archived by the owner on Aug 28, 2020. It is now read-only.
This repository was archived by the owner on Aug 28, 2020. It is now read-only.

Bug in handling cross-ref relations with composite FKs #9

Description

@e1himself

When a cross-ref table has composite FKs, Propel doesn't properly handle xref deletion in doSave methods.

Example

document {
   id
   pk (id) 
}

account {
   user_id
   organisation_id
   pk (user_id, organisation_id)
   fk (user_id) - > user(id)
   fk (organisation_id) -> organisation(id)
}

document_access_xref {
   user_id
   tenant_id
   document_id
   pk (user_id, organisation_id, document_id)
   fk (user_id, organisation_id) -> account(user_id, organisation_id)
   fk (document_id) -> document(id)
}

Propel then generates this code

if ($this->permittedDocumentsScheduledForDeletion !== null) {
    if (! $this->permittedDocumentsScheduledForDeletion->isEmpty()) {
        $pks = [];
        $pk = $this->getPrimaryKey();
        foreach ($this->permittedDocumentsScheduledForDeletion->getPrimaryKeys(false) as $remotePk) {
            $pks[] = [$pk, $remotePk];
        }
        DocumentAccessXrefQuery::create()
          ->filterByPrimaryKeys($pks)
          ->delete($con);
        $this->permittedDocumentsScheduledForDeletion = null;
    }
}

The problem is that filterByPrimaryKeys() expects a flat array of values, while the above code appends composite PK as a nested array ([$pk, $remotePk], where $pk is an array).

Workaround

As temporary possible workaround can be to override filterByPrimaryKeys() in the specific model query class to add an extra normalization pass:

public function filterByPrimaryKeys($keys)
{
    $keys = Arrays::map($keys, function (array $key) {
        if (array_filter($key, 'is_array')) {
            return array_merge(
                ...array_map(fn ($component) => (array) $component, $key),
            ) ;
        }
        return $key;
    });

    return parent::filterByPrimaryKeys($keys);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions