Skip to content

Commit 8be00b9

Browse files
committed
Remove Model::newInstance
1 parent 2793c8c commit 8be00b9

File tree

2 files changed

+7
-32
lines changed

2 files changed

+7
-32
lines changed

src/Model.php

+7-18
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,10 @@ public function __construct(Persistence $persistence = null, array $defaults = [
338338
$this->setDefaults($defaults);
339339

340340
if ($persistence !== null) {
341+
$this->persistence = $persistence;
342+
}
343+
344+
if ($this->persistence !== null) {
341345
$persistence->add($this);
342346
}
343347
}
@@ -1313,10 +1317,12 @@ public function saveAndUnload(array $data = [])
13131317
/**
13141318
* This will cast Model into another class without
13151319
* loosing state of your active record.
1320+
*
1321+
* @param class-string<self> $class
13161322
*/
13171323
public function asModel(string $class, array $options = []): self
13181324
{
1319-
$m = $this->newInstance($class, $options);
1325+
$m = new $class(null, $options);
13201326

13211327
foreach ($this->data as $field => $value) {
13221328
$m->set($field, $value);
@@ -1328,23 +1334,6 @@ public function asModel(string $class, array $options = []): self
13281334
return $m;
13291335
}
13301336

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-
13481337
/**
13491338
* Create new model from the same base class
13501339
* 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([

0 commit comments

Comments
 (0)