# Duplicate a table entry (row)

**URL:** <https://discourse.cakephp.org/t/duplicate-a-table-entry-row/9885>\
**Category:** Need Help\
**Created:** [November 5, 2021, 8:32am UTC](https://discourse.cakephp.org/t/duplicate-a-table-entry-row/9885 "2021-11-05T08:32:44Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![6120](https://avatars.discourse-cdn.com/v4/letter/6/278dde/32.png) [@6120](https://discourse.cakephp.org/u/6120)\
**Post date:** [November 5, 2021, 8:32am UTC](https://discourse.cakephp.org/t/duplicate-a-table-entry-row/9885/1 "2021-11-05T08:32:44Z")

</div>

Is there an simple cakephp way to duplicate an article/table row in the same table ?  
By simple, I mean, of course, not having to list all fields and copy them one by one just like you would do in pure php ? (which is easy but not so straightforward)

---

<div class="post-metadata">

**Author:** ![KevinPfeifer](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/kevinpfeifer/32/3365_2.png) [@KevinPfeifer](https://discourse.cakephp.org/u/KevinPfeifer)\
**Post date:** [November 5, 2021, 1:30pm UTC](https://discourse.cakephp.org/t/duplicate-a-table-entry-row/9885/2 "2021-11-05T13:30:05Z")

</div>

> **[GitHub - riesenia/cakephp-duplicatable: CakePHP plugin for duplicating...](https://github.com/riesenia/cakephp-duplicatable)**
>
> CakePHP plugin for duplicating entities including related data - GitHub - riesenia/cakephp-duplicatable: CakePHP plugin for duplicating entities including related data

Found on [GitHub - FriendsOfCake/awesome-cakephp: A curated list of amazingly awesome CakePHP plugins, resources and shiny things.](https://github.com/FriendsOfCake/awesome-cakephp)

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [November 5, 2021, 2:42pm UTC](https://discourse.cakephp.org/t/duplicate-a-table-entry-row/9885/3 "2021-11-05T14:42:41Z")

</div>

Or just clear the ID and reset `isNew` to true?

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [November 5, 2021, 2:43pm UTC](https://discourse.cakephp.org/t/duplicate-a-table-entry-row/9885/4 "2021-11-05T14:43:27Z")

</div>

Or create a new entity from `$oldEntity->toArray()`, making sure that the ID is not an accessible field?

---

<div class="post-metadata">

**Author:** ![6120](https://avatars.discourse-cdn.com/v4/letter/6/278dde/32.png) [@6120](https://discourse.cakephp.org/u/6120)\
**Post date:** [November 5, 2021, 5:53pm UTC](https://discourse.cakephp.org/t/duplicate-a-table-entry-row/9885/5 "2021-11-05T17:53:42Z")

</div>

Thank you all.  
I will try those ideas and let you know.

---

<div class="post-metadata">

**Author:** ![6120](https://avatars.discourse-cdn.com/v4/letter/6/278dde/32.png) [@6120](https://discourse.cakephp.org/u/6120)\
**Post date:** [November 9, 2021, 7:31am UTC](https://discourse.cakephp.org/t/duplicate-a-table-entry-row/9885/6 "2021-11-09T07:31:19Z")

</div>

Very easy in fact !  
public function duplicate($id)  
{  
$article = $this-\>Articles-\>get($id);  
$newArticle = $this-\>Articles-\>newEntity();  
$this-\>request-\>data = $article-\>toArray();

```
    $this->request->data['name'] = $article->name . " - " . __("copy");
    $this->request->data['slug'] = $article->slug . "-" . __("copy");

    $newArticle = $this->Articles->patchEntity($newArticle, $this->request->data);
    if ($this->Articles->save($newArticle)) {
        $this->Flash->success(__('The article has been duplicated.'));
        return $this->redirect(['controller' => 'articles', 'action' => 'edit', $newArticle->id]);
    } else {
        $this->Flash->error(__('The article could not be duplicated. Please, try again.'));
    }
}

```

Thank you for showing me the way.  
(Did not try the plugin but I keep the link so I can try it later)

---

<div class="post-metadata">

**Author:** ![yousuo](https://avatars.discourse-cdn.com/v4/letter/y/898d66/32.png) [@yousuo](https://discourse.cakephp.org/u/yousuo)\
**Post date:** [November 9, 2021, 9:37am UTC](https://discourse.cakephp.org/t/duplicate-a-table-entry-row/9885/7 "2021-11-09T09:37:04Z")

</div>

you can straight away save it and don’t need to add the extra step of `patchEntity`

```auto
$newArticle = $this->Articles->newEntity( $this->Articles->get($id)->toArray() );
$newArticle->id = null;	// unset id to be safe
$newArticle->setNew(true); // setting entity to new, for safety

$newArticle->name = $newArticle->name . " - " . __("copy");
$newArticle->slug = $newArticle->slug . " - " . __("copy");

$this->Articles->save( $newArticle );

```

---

<div class="post-metadata">

**Author:** ![6120](https://avatars.discourse-cdn.com/v4/letter/6/278dde/32.png) [@6120](https://discourse.cakephp.org/u/6120)\
**Post date:** [November 9, 2021, 9:51am UTC](https://discourse.cakephp.org/t/duplicate-a-table-entry-row/9885/8 "2021-11-09T09:51:02Z")

</div>

Simpler of course.  
But as long as you call newEntity() do we really need $newArticle-\>id = null; and $newArticle-\>setNew(true); ?

---

<div class="post-metadata">

**Author:** ![jarekgol](https://avatars.discourse-cdn.com/v4/letter/j/58956e/32.png) [@jarekgol](https://discourse.cakephp.org/u/jarekgol)\
**Post date:** [November 11, 2021, 10:17am UTC](https://discourse.cakephp.org/t/duplicate-a-table-entry-row/9885/9 "2021-11-11T10:17:11Z")

</div>

You need id = null assuming you have auto\_increment primary key in your db, if you don’t you must assign new id manually. Probably you don’t need isNew() when using newEntity. Just `debug($newArticle)` before `newEntity()` line and check it out.

you can also use php clone:

```auto
$source; //your original from somewhere
$new = clone($source);
$new->id = null;
$new->isNew(true);
`
```

---

<div class="post-metadata">

**Author:** ![6120](https://avatars.discourse-cdn.com/v4/letter/6/278dde/32.png) [@6120](https://discourse.cakephp.org/u/6120)\
**Post date:** [November 17, 2021, 3:36pm UTC](https://discourse.cakephp.org/t/duplicate-a-table-entry-row/9885/10 "2021-11-17T15:36:22Z")

</div>

Thank you all for showing different ways !
