Skip to content

Commit 80a3192

Browse files
committed
Remove Model::newInstance
1 parent 2793c8c commit 80a3192

File tree

4 files changed

+8
-54
lines changed

4 files changed

+8
-54
lines changed

docs/persistence.rst

+2-19
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,7 @@ Start by creating a beforeSave handler for Order::
482482
if ($this->isDirty('ref')) {
483483

484484
if (
485-
$this->newInstance()
485+
(new static())
486486
->addCondition('client_id', $this->get('client_id')) // same client
487487
->addCondition($this->id_field, '!=', $this->getId()) // has another order
488488
->tryLoadBy('ref', $this->get('ref')) // with same ref
@@ -537,7 +537,7 @@ The other, more appropriate option is to re-use a vanilla Order record::
537537
function archive() {
538538
$this->save(); // just to be sure, no dirty stuff is left over
539539

540-
$archive = $this->newInstance();
540+
$archive = (new static());
541541
$archive->load($this->getId());
542542
$archive->set('is_archived', true);
543543

@@ -546,23 +546,6 @@ The other, more appropriate option is to re-use a vanilla Order record::
546546
return $archive;
547547
}
548548

549-
This method may still not work if you extend and use "ActiveOrder" as your
550-
model. In this case you should pass the class to newInstance()::
551-
552-
$archive = $this->newInstance('Order');
553-
// or
554-
$archive = $this->newInstance(new Order());
555-
// or with passing some default properties:
556-
$archive = $this->newInstance([new Order(), 'audit'=>true]);
557-
558-
559-
In this case newInstance() would just associate passed class with the
560-
persistence pretty much identical to::
561-
562-
$archive = new Order($this->persistence);
563-
564-
The use of newInstance() however requires you to load the model which is
565-
an extra database query.
566549

567550
Using Model casting and saveAs
568551
------------------------------

src/Model.php

+3-18
Original file line numberDiff line numberDiff line change
@@ -1313,10 +1313,12 @@ public function saveAndUnload(array $data = [])
13131313
/**
13141314
* This will cast Model into another class without
13151315
* loosing state of your active record.
1316+
*
1317+
* @param class-string<self> $class
13161318
*/
13171319
public function asModel(string $class, array $options = []): self
13181320
{
1319-
$m = $this->newInstance($class, $options);
1321+
$m = new $class(null, $options);
13201322

13211323
foreach ($this->data as $field => $value) {
13221324
$m->set($field, $value);
@@ -1328,23 +1330,6 @@ public function asModel(string $class, array $options = []): self
13281330
return $m;
13291331
}
13301332

1331-
/**
1332-
* Create new model from the same base class
1333-
* as $this.
1334-
*
1335-
* @return static
1336-
*/
1337-
public function newInstance(string $class = null, array $options = [])
1338-
{
1339-
$model = (self::class)::fromSeed([$class ?? static::class], $options);
1340-
1341-
if ($this->persistence) {
1342-
return $this->persistence->add($model); // @phpstan-ignore-line
1343-
}
1344-
1345-
return $model;
1346-
}
1347-
13481333
/**
13491334
* Create new model from the same base class
13501335
* as $this. If you omit $id then when saving

tests/RandomTest.php

-14
Original file line numberDiff line numberDiff line change
@@ -480,20 +480,6 @@ public function testExport()
480480
], $m2->export(['code', 'name'], 'code'));
481481
}
482482

483-
public function testNewInstance()
484-
{
485-
// model without persistence
486-
$m = new Model(null, ['table' => 'order']);
487-
$a = $m->newInstance();
488-
$this->assertFalse(isset($a->persistence));
489-
490-
// model with persistence
491-
$db = new Persistence\Array_();
492-
$m = new Model($db, ['table' => 'order']);
493-
$a = $m->newInstance();
494-
$this->assertTrue(isset($a->persistence));
495-
}
496-
497483
public function testDuplicateSaveNew()
498484
{
499485
$this->setDb([

tests/ReferenceTest.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,11 @@ public function testRefName3()
9898
$db = new Persistence\Array_();
9999
$order = new Model($db, ['table' => 'order']);
100100
$order->addRef('archive', ['model' => function ($m) {
101-
return $m->newInstance(null, ['table' => $m->table . '_archive']);
101+
return new $m(null, ['table' => $m->table . '_archive']);
102102
}]);
103103
$this->expectException(Exception::class);
104104
$order->addRef('archive', ['model' => function ($m) {
105-
return $m->newInstance(null, ['table' => $m->table . '_archive']);
105+
return new $m(null, ['table' => $m->table . '_archive']);
106106
}]);
107107
}
108108

@@ -112,7 +112,7 @@ public function testCustomRef()
112112

113113
$m = new Model($p, ['table' => 'user']);
114114
$m->addRef('archive', ['model' => function ($m) {
115-
return $m->newInstance(null, ['table' => $m->table . '_archive']);
115+
return new $m(null, ['table' => $m->table . '_archive']);
116116
}]);
117117

118118
$this->assertSame('user_archive', $m->ref('archive')->table);

0 commit comments

Comments
 (0)