# Duplicate data into another table

**URL:** <https://discourse.cakephp.org/t/duplicate-data-into-another-table/9397>\
**Category:** Need Help\
**Created:** [June 3, 2021, 12:10am UTC](https://discourse.cakephp.org/t/duplicate-data-into-another-table/9397 "2021-06-03T00:10:35Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Foggies](https://avatars.discourse-cdn.com/v4/letter/f/a183cd/32.png) [@Foggies](https://discourse.cakephp.org/u/Foggies)\
**Post date:** [June 3, 2021, 12:10am UTC](https://discourse.cakephp.org/t/duplicate-data-into-another-table/9397/1 "2021-06-03T00:10:35Z")

</div>

Hello bakers 🙂

I’m having a problem saving duplicate data. I’ve two (2) tables: users [id,fullname,email] and duplicates [id,fullname,email].  
What I want to do is to duplicate input data from users add form into the duplicates table. Normally, we can use: duplicates.0.fullname & duplicates.0.email field in Users/add.php but I don’t want to have a duplicate input field. How can I save the users [fullname,email] into a duplicates table from the controller?  
I tried the following codes to get the value from user.fullname & user.email but how can i save it?

```auto
public function add()
{
	$user = $this->Users->newEmptyEntity();
	if ($this->request->is('post')) {
	$duplicate_fullname = $this->request->getData('fullname'); //getvalue from user.fullname
	$duplicate_email = $this->request->getData('email'); //tet value from user.email
		$user = $this->Users->patchEntity($user, $this->request->getData());
		if ($this->Users->save($user)) {
			$this->Flash->success(__('The user has been saved.'));

			return $this->redirect(['action' => 'index']);
		}
		$this->Flash->error(__('The user could not be saved. Please, try again.'));
	}
	$this->set(compact('user'));
}

```

Thank you

---

<div class="post-metadata">

**Author:** ![Jawfin](https://avatars.discourse-cdn.com/v4/letter/j/dbc845/32.png) [@Jawfin](https://discourse.cakephp.org/u/Jawfin)\
**Post date:** [June 3, 2021, 12:39am UTC](https://discourse.cakephp.org/t/duplicate-data-into-another-table/9397/2 "2021-06-03T00:39:30Z")

</div>

After its saved correctly

> [@Foggies](#):
>
> `if ($this->Users->save($user)) {`

you’ll need to load your duplicate model and add the record there. If you need source code on how to just ask; but conceptually that’s where it goes. Obviously you’ll need an actual duplicate model (unless its _exactly_ the same as your users table, you may be able to sub-class its object - but I don’t know how to do that in Cake!).

---

<div class="post-metadata">

**Author:** ![dreamingmind](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/dreamingmind/32/1857_2.png) [@dreamingmind](https://discourse.cakephp.org/u/dreamingmind)\
**Post date:** [June 3, 2021, 12:43am UTC](https://discourse.cakephp.org/t/duplicate-data-into-another-table/9397/3 "2021-06-03T00:43:11Z")

</div>

If the Duplicates table is associated with the User table (even distantly) you can get to it along the association chain:

```php
$DuplicateTable = $this->Users->NextAssociaton->Duplicates;

```

Or the controllers should allow this:

```php
$DuplicatesTable = $this->getTableLocator()->get('Duplicates');

```

You can make any class support the ‘locator’ syntax by using the `LocatorAwareTrait` (Cake 4x).

Once you have a DuplicatesTable instance, patch your data and save it.

```php
$patchData = ['name' => $duplicate_fullname, 'email' => $duplicate_email];

$duplicate = $DuplicatesTable->newEntity($patchData);

if (!$duplicate->hasErrors() && DuplicatesTable->save($duplicate) {
   //all is well
}
else {
  //problems can be found enumerated at $duplicate->getErrors();
}

```

---

<div class="post-metadata">

**Author:** ![Foggies](https://avatars.discourse-cdn.com/v4/letter/f/a183cd/32.png) [@Foggies](https://discourse.cakephp.org/u/Foggies)\
**Post date:** [June 3, 2021, 8:02am UTC](https://discourse.cakephp.org/t/duplicate-data-into-another-table/9397/4 "2021-06-03T08:02:36Z")

</div>

Thank you. I really appreciate your reply and solution. Problem solved 🙂

The following codes is the solution:

```auto
public function add()
{
	$user = $this->Users->newEmptyEntity();
	if ($this->request->is('post')) {
		$user = $this->Users->patchEntity($user, $this->request->getData());
			$duplicate_fullname = $this->request->getData('fullname');
			$duplicate_email = $this->request->getData('email');

			$duplicatesTable = TableRegistry::getTableLocator()->get('Duplicates');
			$duplicateData = ['fullname' => $duplicate_fullname, 'email' => $duplicate_email];

			$duplicate = $duplicatesTable->newEntity($duplicateData);
		
		if ($this->Users->save($user) && $duplicatesTable->save($duplicate)) {
			$this->Flash->success(__('The user has been saved.'));
			return $this->redirect(['action' => 'index']);
		}
		$this->Flash->error(__('The user could not be saved. Please, try again.'));
	}
	$this->set(compact('user'));
}

```

I also found that if in table duplicates have user\_id, then you need to execute the save after $this-\>Users-\>save so that you can get the user id using $user-\>id.

Hope this might be helpful for others 🙂

---

<div class="post-metadata">

**Author:** ![Jawfin](https://avatars.discourse-cdn.com/v4/letter/j/dbc845/32.png) [@Jawfin](https://discourse.cakephp.org/u/Jawfin)\
**Post date:** [June 3, 2021, 3:11pm UTC](https://discourse.cakephp.org/t/duplicate-data-into-another-table/9397/5 "2021-06-03T15:11:35Z")

</div>

That is how I would have done it, but I sense there is a proper Cake solution, from what DreamingMind posted, where the tables are associated and the Duplicate is created along the lines of an Object Oriented approach. Meaning, if you added a new Users record _anywhere_, not just in `add()` it’ll still create the Duplicate record without explicit code there.

---

<div class="post-metadata">

**Author:** ![dreamingmind](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/dreamingmind/32/1857_2.png) [@dreamingmind](https://discourse.cakephp.org/u/dreamingmind)\
**Post date:** [June 3, 2021, 3:42pm UTC](https://discourse.cakephp.org/t/duplicate-data-into-another-table/9397/6 "2021-06-03T15:42:18Z")

</div>

> [@Jawfin](#):
>
> where the tables are associated and the Duplicate is created along the lines of an Object Oriented approach

Yes the data can be [saved in one step](https://book.cakephp.org/4/en/orm/saving-data.html#saving-with-associations). Modifying @Foggies code:

```php

//this assumes Users->hasOne('Duplicates') 

public function add()
{
  $user = $this->Users->newEmptyEntity();
  if ($this->request->is('post')) {

     $patchData = $this->request->getData());
     $patchData['duplicate'] = [
        'fullname' => $this->request->getData('fullname');
        'email' => $this->request->getData('email')
     ];
     $user = $this->Users->patchEntity($user, $patchData);

     if ($this->Users->save($user)) {
        $this->Flash->success(__('The user has been saved.'));
        return $this->redirect(['action' => 'index']);
     }

     $this->Flash->error(__('The user could not be saved. Please, try again.'));
  }
  $this->set(compact('user'));
}

```

Cake will create User and use the new id to link Duplicate. By default this save will be done in a transaction, so no worries there.

The key to making this work lies in proper naming of the node where the associated data is attached; in this case ‘duplicate’.

```php
//in the array
$patchData['duplicate']
//later in the entity
$user->duplicate
//hasOne makes the node name singular by default. 
//hasMany would make it plural

```

As a general rule, if you want to save associated data but aren’t sure how to assemble the patch array (and the subsequent entity), do a query of the association and debug the results-\>toArray(). This is the structure you should recreate for your save.

```php
$result = $this->Users->find()
   ->where('User.id' => $id)
   ->contain('Duplicates')
   ->toArray();

debug($result)->toArray();

```
