@@ -338,6 +338,10 @@ public function __construct(Persistence $persistence = null, array $defaults = [
338
338
$ this ->setDefaults ($ defaults );
339
339
340
340
if ($ persistence !== null ) {
341
+ $ this ->persistence = $ persistence ;
342
+ }
343
+
344
+ if ($ this ->persistence !== null ) {
341
345
$ persistence ->add ($ this );
342
346
}
343
347
}
@@ -1313,10 +1317,12 @@ public function saveAndUnload(array $data = [])
1313
1317
/**
1314
1318
* This will cast Model into another class without
1315
1319
* loosing state of your active record.
1320
+ *
1321
+ * @param class-string<self> $class
1316
1322
*/
1317
1323
public function asModel (string $ class , array $ options = []): self
1318
1324
{
1319
- $ m = $ this -> newInstance ( $ class , $ options );
1325
+ $ m = new $ class( null , $ options );
1320
1326
1321
1327
foreach ($ this ->data as $ field => $ value ) {
1322
1328
$ m ->set ($ field , $ value );
@@ -1328,23 +1334,6 @@ public function asModel(string $class, array $options = []): self
1328
1334
return $ m ;
1329
1335
}
1330
1336
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
-
1348
1337
/**
1349
1338
* Create new model from the same base class
1350
1339
* as $this. If you omit $id then when saving
0 commit comments