Skip to content

Commit e399996

Browse files
committed
[fix] Model::duplicate
1 parent 1fb2c24 commit e399996

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

src/Model.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -1178,12 +1178,14 @@ public function reload()
11781178
*/
11791179
public function duplicate($newId = null)
11801180
{
1181-
$this->setId(null);
1181+
$this->entityId = null;
11821182

11831183
if ($this->id_field) {
11841184
$this->setId($newId);
11851185
}
11861186

1187+
$this->dirty = $this->get();
1188+
11871189
return $this;
11881190
}
11891191

tests/RandomTest.php

+43
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,49 @@ public function testNewInstance()
497497
$this->assertTrue(isset($a->persistence));
498498
}
499499

500+
public function testDuplicate1()
501+
{
502+
$this->setDb([
503+
'rate' => [
504+
['dat' => '18/12/12', 'bid' => 3.4, 'ask' => 9.4],
505+
['dat' => '12/12/12', 'bid' => 8.3, 'ask' => 9.2],
506+
],
507+
]);
508+
509+
$db = new Persistence\Sql($this->db->connection);
510+
$m = new Model_Rate($db);
511+
512+
$m->load(1)->duplicate()->save();
513+
514+
$this->assertSame([
515+
['dat' => '18/12/12'],
516+
['dat' => '12/12/12'],
517+
['dat' => '18/12/12'],
518+
], $m->export(['dat']));
519+
}
520+
521+
public function testDuplicate2()
522+
{
523+
$this->setDb([
524+
'rate' => [
525+
['dat' => '18/12/12', 'bid' => 3.4, 'ask' => 9.4],
526+
['dat' => '12/12/12', 'bid' => 8.3, 'ask' => 9.2],
527+
],
528+
]);
529+
530+
$db = new Persistence\Sql($this->db->connection);
531+
$m = new Model_Rate($db);
532+
533+
$m->load(1)->duplicate(2)->save();
534+
535+
$this->assertSame(2, $m->getId());
536+
537+
$this->assertSame([
538+
['id' => '1', 'dat' => '18/12/12', 'bid' => '3.4', 'ask' => '9.4'],
539+
['id' => '2', 'dat' => '18/12/12', 'bid' => '3.4', 'ask' => '9.4'],
540+
], $m->export());
541+
}
542+
500543
public function testTableNameDots()
501544
{
502545
$d = new Model($this->db, 'db2.doc');

0 commit comments

Comments
 (0)