-
Notifications
You must be signed in to change notification settings - Fork 18
handling composite key support #38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -35,7 +35,8 @@ class DuplicatableBehavior extends Behavior | |
| 'set' => [], | ||
| 'prepend' => [], | ||
| 'append' => [], | ||
| 'saveOptions' => [] | ||
| 'saveOptions' => [], | ||
| 'preserveKeyFromCompositeKey' => '' | ||
| ]; | ||
|
|
||
| /** | ||
|
|
@@ -179,12 +180,14 @@ protected function _modifyEntity(EntityInterface $entity, $object) | |
| if ($object instanceof BelongsToMany) { | ||
| unset($entity->_joinData); | ||
| } else { | ||
|
|
||
| // unset primary key | ||
| unset($entity->{$object->getPrimaryKey()}); | ||
| $this->removePrimaryKey($entity, $object); | ||
|
|
||
| // unset foreign key | ||
| if ($object instanceof Association) { | ||
| unset($entity->{$object->getPrimaryKey()}); | ||
| // unset primary key | ||
| $this->removePrimaryKey($entity, $object); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -329,4 +332,34 @@ protected function _doAction($action, EntityInterface $entity, $prop, $value = n | |
| break; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Removes the primary key from the entity being duplicated. If the entity has a composite key, you must | ||
| * specify which field we wish to preserve | ||
| * | ||
| * @param EntityInterface $entity | ||
| * @param $object | ||
| * @throws \Exception | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. return void missing maybe here? |
||
| */ | ||
| protected function removePrimaryKey(EntityInterface $entity, $object) | ||
| { | ||
| if (is_array($object->getPrimaryKey())) { | ||
|
|
||
| $compositeKey = $object->getPrimaryKey(); | ||
|
||
|
|
||
| $keyToPreserve = $this->getConfig('preserveKeyFromCompositeKey'); | ||
|
|
||
| if(!in_array($keyToPreserve, $compositeKey)){ | ||
| throw new \Exception('You must specify which key to preserve when duplicating composite keys'); | ||
| } | ||
|
|
||
| foreach ($compositeKey as $pkField){ | ||
| if ($pkField != $keyToPreserve) { | ||
| unset($entity->{$pkField}); | ||
| } | ||
| } | ||
| } else { | ||
| unset($entity->{$object->getPrimaryKey()}); | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this this a string, what if composite key consistents of 3 fields and 2 need to be removed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done