atk4\data\Model::duplicate PHP Method

duplicate() public method

Keeps the model data, but wipes out the ID so when you save it next time, it ends up as a new record in the database.
public duplicate ( mixed | null $new_id = null )
$new_id mixed | null
    public function duplicate($new_id = null)
    {
        $this->id = null;
        $this[$this->id_field] = $new_id;
        return $this;
    }

Usage Example

Beispiel #1
0
 public function testTypeCustom1()
 {
     $a = ['types' => [['date' => '2013-02-20', 'datetime' => '2013-02-20 20:00:12', 'time' => '12:00:50', 'b1' => 'Y', 'b2' => 'N', 'integer' => '2940', 'money' => '8.20', 'float' => '8.202343', 'rot13' => 'uryyb jbeyq']]];
     $this->setDB($a);
     $db = new Persistence_SQL($this->db->connection);
     date_default_timezone_set('Asia/Seoul');
     $m = new Model($db, ['table' => 'types']);
     $m->addField('date', ['type' => 'date', 'dateTimeClass' => '\\atk4\\data\\tests\\MyDate']);
     $m->addField('datetime', ['type' => 'datetime', 'dateTimeClass' => '\\atk4\\data\\tests\\MyDateTime']);
     $m->addField('time', ['type' => 'time', 'dateTimeClass' => '\\atk4\\data\\tests\\MyTime']);
     $m->addField('b1', ['type' => 'boolean', 'enum' => ['N', 'Y']]);
     $m->addField('b2', ['type' => 'boolean', 'enum' => ['N', 'Y']]);
     $m->addField('money', ['type' => 'money']);
     $m->addField('float', ['type' => 'float']);
     $m->addField('integer', ['type' => 'integer']);
     $rot = function ($v) {
         return str_rot13($v);
     };
     $m->addField('rot13', ['typecast' => [$rot, $rot]]);
     $m->load(1);
     $this->assertSame('hello world', $m['rot13']);
     $this->assertSame('1', $m->id);
     $this->assertSame('1', $m['id']);
     $this->assertEquals('2013-02-21 05:00:12', (string) $m['datetime']);
     $this->assertEquals('2013-02-20', (string) $m['date']);
     $this->assertEquals('12:00:50', (string) $m['time']);
     $this->assertEquals(true, $m['b1']);
     $this->assertEquals(false, $m['b2']);
     $m->duplicate()->save()->delete(1);
     $a = ['types' => [2 => ['id' => '2', 'date' => '2013-02-20', 'datetime' => '2013-02-20 20:00:12', 'time' => '12:00:50', 'b1' => 'Y', 'b2' => 'N', 'integer' => '2940', 'money' => '8.20', 'float' => '8.202343', 'rot13' => 'uryyb jbeyq']]];
     $this->assertEquals($a, $this->getDB());
 }