Skip to content

Cannot duplicate entities with a composite key #37

@birdy247

Description

@birdy247

Table: AdditionalParticipantValues
$this->setPrimaryKey(['addition_id', 'participant_id']);

    $this->belongsTo('Participants', [
        'foreignKey' => 'participant_id',
        'joinType' => 'INNER'
    ]);

Table: Participants
// add Duplicatable behavior
$this->addBehavior('Duplicatable.Duplicatable', [
// table finder
'finder' => 'all',
// duplicate also items and their properties
'contain' => ['AdditionalParticipantValues', 'ParticipantAffiliations'],
'saveOptions' => ['checkRules' => false]
]);

    $this->hasMany('AdditionalParticipantValues', [
        'foreignKey' => 'participant_id'
    ]);

In the _modifyEntity, it attempts to unset the primary key of any associations. This will cause an Array to String exception when using a composite key (which is an array). As workaround, I added the following check in the _modifyEntity

if(is_array($object->getPrimaryKey())){ unset($entity->{$object->getPrimaryKey()[1]}); }else{ unset($entity->{$object->getPrimaryKey()}); }

It assumes that a composite key will always be made up of 2, and it also assumes that we always preserve the 1st key (and unsets the 2nd).

This achieves what I want i.e. the fields are saved with the correct ID's.

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