atk4\data\Model::newInstance PHP Method

newInstance() public method

Create new model from the same base class as $this.
public newInstance ( string $class = null, array $options = [] ) : Model
$class string
$options array
return Model
    public function newInstance($class = null, $options = [])
    {
        if ($class === null) {
            $class = get_class($this);
        }
        $m = $this->persistence->add($class, $options);
        return $m;
    }

Usage Example

Example #1
0
 public function testCustomRef()
 {
     $a = [];
     $p = new \atk4\data\Persistence_Array($a);
     $m = new Model($p, ['table' => 'user']);
     $m->addRef('archive', function ($m) {
         return $m->newInstance(null, ['table' => $m->table . '_archive']);
     });
     $this->assertEquals('user_archive', $m->ref('archive')->table);
 }