1
votes

I'm new to datamapper. I have a problem on trying to duplicate a result into a new id.

This is a simplified table for my database:

Job Table

| id | property_id | name | type |
| 1  | 1           | abc  | i    |
| 2  | 2           | def  | ii   |

Property Table

| id | job_id | size |
| 1  | 1      | 90   |
| 2  | 2      | 40   |


How can I automatically duplicate a new job based on job id 1 into new job/property id like

Job Table

| id | property_id | name | type |
| 1  | 1           | abc  | i    |
| 2  | 2           | def  | ii   |
| 3  | 3           | abc  | i    |

Property Table

| id | job_id | size |
| 1  | 1      | 90   |
| 2  | 2      | 40   |
| 3  | 3      | 90   |

Thanks for helping! :)

2

2 Answers

3
votes

In the documentation for DataMapper Overzealous Edition: http://datamapper.wanwizard.eu/pages/clonecopy.html There's clone and copy, copy will clear the id. Here's their example, just skip the part of making changes:

// Let's save a new hosting plan
$p = new Plan();

$p->name = 'The 100GB Plan';
$p->storage = 1000;
$p->bandwidth = 2000;
$p->databases = 5;
$p->domains = 5;
$p->emails = 50;

$p->save();

// Now, lets make a copy of that saved plan and base a new one off of it
$p = $p->get_copy();

// Change only what we need to
$p->name = 'The Big 150GB Plan';
$p->storage = 1500;
$p->bandwidth = 2500;

// And now save a new record
$p->save();
1
votes

You can also just modify the object you retrieve, and then use save_as_new() to save it as a new record.